我认为最好用一个例子来说明。使用这样的结构:
typedef struct {
int a;
char b;
} Foo;
定义一个宏:
#define Foo(A, B) (Foo){ A, B }
然后可以这样使用:
Foo foo;
foo = Foo(1, 'c');
我已经使用这种模式一段时间了,对我来说,这是一种非常优雅的结构初始化方式。它适用于 GCC 和 Clang(我认为还有 TCC),但在一些鲜为人知的 C 编译器(如 Plan 9 的 C 编译器)中它不起作用。
C 标准对这个主题有何规定?
最佳答案
摘自 C11 标准的 §6.10.3.10:
Each subsequent instance of the function-like macro name followed by a ( as the next preprocessing token introduces the sequence of preprocessing tokens that is replaced by the replacement list in the definition (an invocation of the macro).
所以,不,你不应该担心。只有在宏名称后跟左括号 (.
关于c - 根据 C 标准,带参数的宏可能会与标识符发生冲突吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50787755/