我正在将一些代码移至 GCC 4.7(从 4.6 开始)并遇到一些编译器错误并发现 GCC 4.7 porting guide 中记录的问题:
User-defined literals and whitespace
The C++ compiler in ISO C11 mode
std={c++11,c++0x,gnu++11,gnu++0x}
supports user defined literals, which are incompatible with some valid ISO C++03 code.In particular, whitespace is now needed after a string literal and before something that could be a valid user defined literal. Take the valid ISO C++03 code
const char *p = "foobar"__TIME__;
In C++03, the TIME macro expands to some string literal and is concatenated with the other one. In C++11
__TIME__
isn't expanded, insteadoperator "" __TIME__
is being looked up, resulting in the following diagnostic:error: unable to find string literal operator ‘operator"" __TIME__’
This applies to any string literal followed without whitespace by some macro. To fix, just add some whitespace between the string literal and the macro name.
虽然我可以修复错误,但我想知道为什么我必须这样做。 __TIME__
是一个宏,所以 "something"__TIME__
在预处理阶段,因此编译器永远不会有机会将其视为 operator ""
.
这种行为是标准批准的还是一个错误?
最佳答案
问题是 "foobar"__TIME__
不再标记为预处理器标记 "foobar"
后跟 __TIME__
。
预处理器标记“具有关键字、标识符、文字、运算符或标点符号的词法形式。”添加用户定义的文字会改变作为预处理器标记的词法。现在 "foobar"__TIME__
是一个单独的 user-defined-character-literal 预处理器标记,因此当翻译的第 4 阶段发生时,它将替换 __TIME__带有
,没有 "15:52:03"
的__TIME__
标记可以这样替换。
是的,标准中规定了这种行为。
由于 cinttypes 宏,受此影响的代码似乎比委员会意识到的要多,他们正在考虑解决这个问题。一些编译器已经着手处理 cinttypes 的问题,但并没有为您解决这种使用 __TIME__
的问题。我认为您最好的选择是更改代码。
关于c++ - g++ 4.7 将运算符 ""评估为宏扩展的兄弟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11909806/