错误:预处理令牌无效

时间:2013-06-19 16:54:24

标签: c++11

#define A(a)  "str" ## a ## test
A(_)

根据C ++ 11标准中的17.6.4.3.5

  

不以下划线开头的文字后缀标识符保留用于将来的标准化。

上面的代码应该生成"str"_test,这是一个有效预处理令牌,它是类user-defined-string-literal

通过-E在预处理器模式下运行时,

clang 3.0会产生错误。

clang给出:

pasting formed '"str"_', an invalid preprocessing token
A(_)
^
note: expanded from:
#define A(a)  "str" ## a ## test
                    ^

"str"_test
1 error generated.

我不明白是什么步骤决定结果不是无效的预处理令牌。

注意:我正在编写一个c ++ 11预处理器。

1 个答案:

答案 0 :(得分:2)

认为代码是有效的C ++ 11;看起来你正在使用一个不完整的C ++ 11支持的编译器。

使用g ++版本4.7.2(带-std=c++11),这个人为的程序:

#include <cstddef>
#include <iostream>

#define A(a)  "str" ## a ## test

const char* operator"" _test(const char s[4], size_t size) {
    return s;
}

int main() {
    std::cout << A(_) << "\n";
}

编译时没有错误并产生此输出:

str

clang ++ 3.0版不太开心;除了其他错误,它说:

c.cpp:11:18: error: pasting formed '"str"_', an invalid preprocessing token
    std::cout << A(_) << "\n";
                 ^
c.cpp:4:21: note: expanded from:
#define A(a)  "str" ## a ## test
                    ^