在 VS2013 与 VS2017 中的 printf 函数中使用宏

Using macros in printf function in VS2013 vs VS2017

我在我的源代码中定义了这个宏

#define UINT_08X_FORMAT   "%08X"

我需要像这样在 printf 中使用上面的内容:

printf("Test - "UINT_08X_FORMAT"", 50);

它在 VS2013 中编译并工作正常,而在 VS2017 中,它抛出以下编译错误。

invalid literal suffix 'UINT_08X_FORMAT'; literal operator or literal operator template 'operator ""UINT32_FORMAT' not found

如何在printf中使用宏。

Note: I dont want to change the macro definition as it works fine with VS2013. I need a common solution which will work on both VS2013 and VS2017.

C++11 添加了对用户定义文字 (UDL) 的支持,这是通过向其他文字(在本例中为字符串文字)添加后缀来触发的。您可以通过在宏名称周围添加空格来强制较新的 C++ 编译器将其视为单独的标记而不是 UDL 后缀来克服它:

printf("Test - " UINT_08X_FORMAT "", 50);

查看来自 http://en.cppreference.com/w/cpp/language/user_literal 的注释:

Since the introduction of user-defined literals, the code that uses format macro constants for fixed-width integer types with no space after the preceding string literal became invalid: std::printf("%"PRId64"\n",INT64_MIN); has to be replaced by std::printf("%" PRId64"\n",INT64_MIN);

Due to maximal munch, user-defined integer and floating point literals ending in p, P, (since C++17) e and E, when followed by the operators + or -, must be separated from the operator with whitespace in the source