c++ - ##语法是什么?

标签 c++ c-preprocessor

<分区>

我通读了一些示例代码,并了解了这个我以前从未见过的 ## 语法。代码示例如下:

#define DEF_MAKE_BT_1_COMP(BT)\
inline i_##BT make_i_##BT(i_##BT::vtype x) { return make_##BT(x); }

#define DEF_MAKE_BT2(T)         \
DEF_MAKE_BT_1_COMP(T##1);           \
DEF_MAKE_BT_2_COMP(T##2);

DEF_MAKE_BT2(double);

当我在最后一行出错时,“make_double1”未定义。有人以前见过这种语法吗?

最佳答案

## 是连接两个标记的预处理器运算符。

来自 http://en.cppreference.com/w/cpp/preprocessor/replace

A ## operator between any two successive identifiers in the replacement-list runs parameter replacement on the two identifiers (which are not macro-expanded first) and then concatenates the result. This operation is called "concatenation" or "token pasting". Only tokens that form a valid token together may be pasted: identifiers that form a longer identifier, digits that form a number, or operators + and = that form a +=. A comment cannot be created by pasting / and * because comments are removed from text before macro substitution is considered. If the result of concatenation is not a valid token, the behavior is undefined.

关于c++ - ##语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30160018/

相关文章:

C++:尝试在单个 for 循环中使用 getlline() 填充多个 vector 时, vector 下标超出范围

c++ - 为什么链接顺序会影响使用宏的测试的测试结果?

c++ - 将 HTTP header 添加到 Windows 上的所有传出数据包?

c++ - 如何将泛型方法传递给静态函数

c++ ShellExecute "open"在已经打开的窗口中

c++ - 为什么调用closesock后socket accept函数不释放?

C(嵌入式): Giving source file unique ID's

c++ - C/C++ 宏字符串连接

c - #define 中 #if 的最佳替代方法是什么

c++ - 每个标记的可变宏扩展