c++ - #define 中的## 是什么意思?

标签 c++ c c-preprocessor

这行是什么意思?特别是,## 是什么意思?

#define ANALYZE(variable, flag)     ((Something.##variable) & (flag))

编辑:

还是有点懵。如果没有 ## 会是什么结果?

最佳答案

A little bit confused still. What will the result be without ##?

通常您不会注意到任何差异。但是, 是有区别的。假设 Something 的类型是:

struct X { int x; };
X Something;

然后看看:

int X::*p = &X::x;
ANALYZE(x, flag)
ANALYZE(*p, flag)

没有标记连接运算符##,它扩展为:

#define ANALYZE(variable, flag)     ((Something.variable) & (flag))

((Something. x) & (flag))
((Something. *p) & (flag)) // . and * are not concatenated to one token. syntax error!

通过标记串联,它扩展为:

#define ANALYZE(variable, flag)     ((Something.##variable) & (flag))

((Something.x) & (flag))
((Something.*p) & (flag)) // .* is a newly generated token, now it works!

重要的是要记住预处理器对预处理器标记进行操作,而不是对文本进行操作。所以如果你想连接两个标记,你必须明确地说出来。

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

相关文章:

c++ - const char* 作为模板的参数

c - “program.exe has stopped running”尝试了一切,但无济于事?

c - 这个C Actor 有什么问题

c++ - "more than one instance of overloaded function "标准::战俘 "matches the argument list"

c - 如何编写一个宏来访问 C 中内存中的数据?

c++ - 如何使用迭代器作为模板参数并返回其值?

c++ - 如何模拟 alignas(T)?

c++ - 插入 std::map<int, std::vector<int>> 的短函数

c - 内存中的代码向哪个方向执行?

c - inline和#define在实践中有什么区别?