gpt4 book ai didi

c++11 - 错误 : invalid preprocessing token

转载 作者:行者123 更新时间:2023-12-01 03:56:10 29 4
gpt4 key购买 nike

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

根据 17.6.4.3.5在 C++11 标准中

Literal suffix identifiers that do not start with an underscore are reserved for future standardization.



上面的代码应该产生 "str"_test这是一个 有效 预处理 token 及其类 user-defined-string-literal .

通过 -E 在预处理器模式下运行时,clang 3.0 会产生错误.

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

"str"_test
1 error generated.

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

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

最佳答案

我认为代码是有效的 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
^

关于c++11 - 错误 : invalid preprocessing token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17197000/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com