gpt4 book ai didi

C++预处理器##运算符

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:57 25 4
gpt4 key购买 nike

怎么了?我希望 xconcat 行能够工作。

#define concat(a,b) a ## b
#define xconcat(a,b) concat(a,b)

int main() {
xconcat(xconcat(boost::variant<,int), >) y;
boost::variant<int> x;
return 0;
}

g++ -E x.cpp

# 1 "x.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "x.cpp"



int main() {
x.cpp:5:1: error: pasting "<" and "int" does not give a valid preprocessing token
x.cpp:5:1: error: pasting "int" and ">" does not give a valid preprocessing token
boost::variant<int > y;
boost::variant<int> x;
return 0;
}

最佳答案

token 粘贴运算符在技术上不能用于粘贴最终不会成为 token 的内容。 GCC 强制执行该限制,而其他一些编译器则不强制执行(## 运算符似乎只是执行字符串的基本连接,然后稍后进行标记化)。

来自 C++11 16.3.3/3 “## 运算符”:

If the result is not a valid preprocessing token, the behavior is undefined.

几乎所有可追溯到 C90 的 C 和 C++ 标准中都使用相同的语言。

在您的情况下,您不需要使用 token 粘贴,因为无论如何您都在处理单独的 token :

#define yyconcat(a,b) a b
#define yconcat(a,b) yyconcat(a,b)

int main() {
yconcat(yconcat(boost::variant<,int), >) y;
boost::variant<int> x;
return 0;
}

g++ -E so-test.c

C:\so-test>g++ -E so-test.c
# 1 "so-test.c"
# 1 "<command-line>"
# 1 "so-test.c"



int main() {
boost::variant< int > y;
boost::variant<int> x;
return 0;
}

关于C++预处理器##运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35713953/

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