我正在实现 X 宏,但我遇到了一个简单的宏扩展问题。this
这个宏(见下文)通过包含在文章中被用于几个宏使用示例。编译器给出了一条错误消息,但我可以通过在 GCC 编译器中使用-E
标志来查看有效的 C 代码。
宏 X-list 定义如下:
#define LIST \
X(red, "red") \
X(blue, "blue") \
X(yellow, "yellow")
进而:
#define X(a, b) foo.##a = -1;
LIST;
#undef X
但是 gcc 给出了以下错误消息:
lixo.c:42:1: error: pasting "." and "red" does not give a valid preprocessing token
lixo.c:42:1: error: pasting "." and "blue" does not give a valid preprocessing token
lixo.c:42:1: error: pasting "." and "yellow" does not give a valid preprocessing token
就像我说的,我可以通过-E
在 gcc 上使用 switch 来查看有效的 C 代码:
lixo.c:42:1: error: pasting "." and "red" does not give a valid preprocessing token
lixo.c:42:1: error: pasting "." and "blue" does not give a valid preprocessing token
lixo.c:42:1: error: pasting "." and "yellow" does not give a valid preprocessing token
foo.red = -1; foo.blue = -1; foo.yellow = -1;;
什么是有效的预处理令牌?有人可以解释一下吗?
(在您说“为什么不只是初始化或memset()
?”之前,这不是我的真实代码。)