c - 粘贴标记时意外的预定义宏行为

标签 c gcc

以下(使用 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/

相关文章:

c - 将命令行参数保存为 C 中的全局变量

C编程警告: array subscript has type 'char' [-Wchar-subscripts]

c++ - GNU C++ 和 Clang 中的模板实例化

gcc - gnu arm 汇编器命令行宏失败并显示 "Invalid identifier for .ifdef"

gcc - 编译器总是生成汇编代码吗?

c - 将 struct var 分配给函数的 retval 时为 "Incompatible types"

c++ - segmentation 故障的常见原因的明确列表

c++ - QtCreator CMake 项目 - 如何显示所有项目文件

c - 可用于 gtk+ 开发的 IDE 有哪些

c++ - complex.h 的 gcc 编译器选项