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

C++ preprocessor ## operator

怎么了?我希望 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;
}

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

来自 C++11 16.3.3/3 "The ## operator":

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

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

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

#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 所以-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;
}