以下(使用 gcc -E blah.c
测试):
#define UNUSED(type) type UNUSED_ ## __COUNTER__
UNUSED(char const *)
UNUSED(int)
生成:
char const * UNUSED__COUNTER__
int UNUSED__COUNTER__
我期待:
char const * UNUSED0
int UNUSED1
我试过调用另一个宏,将参数括在方括号中,但无济于事。
如果我不粘贴 token ,它似乎工作正常。
documentation特别提到在 token 粘贴中使用__COUNTER__
。
我做错了什么?
最佳答案
用 gcc 4.4 进行试验,这有效:
#define UNUSED(type) UNUSED_(type, __COUNTER__)
#define UNUSED_(type, counter) UNUSED__(type, counter)
#define UNUSED__(type, counter) type UNUSED_ ## counter
UNUSED(char const *)
UNUSED(int)
但是即使我去掉一个级别的中间体也是行不通的。
关于c - 粘贴标记时意外的预定义宏行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3488021/