c++ - g++ 4.7 将运算符 ""评估为宏扩展的兄弟

标签 c++ compilation user-defined-literals gcc4.7

我正在将一些代码移至 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, instead operator "" __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/

相关文章:

c++ - typeid(复杂<double>(0.0,1.0)) != typeid(1.0i)

c++ - 为什么此模板签名不能用作使用引号的字符串文字?

c++ - Intel 2015 编译器错误,RAII 破坏不正确,这是错误还是我做错了什么?

c++ - 有没有办法在 std::array 之间切换

c - 瑞萨电子 GCC 链接器共享代码错误

涉及结构的编译错误

C++矩阵类不同的维度

c++ - Windows KMFD HelloWorld 驱动程序部署任务失败

java - 如何在 Linux 中从下载的源代码创建 Java 包

c++ - 我可以在运行时调用选择调用哪个用户定义文字的逻辑吗?