gpt4 book ai didi

c - Variadic 宏扩展出错了

转载 作者:行者123 更新时间:2023-12-01 13:12:15 27 4
gpt4 key购买 nike

考虑以下代码

#define COMB(F, ...) F(__VA_ARGS__)


#define ADD(X, Y) (X + Y)


int foo() {
return COMB(ADD, 1, 2);
}

我在 Godbolt 上做了一些实验。 Microsoft VS v19.22(带有/E 标志)在预处理宏时失败。它给出了以下错误

int foo() {

return (1, 2 + );

}

example.cpp

<source>(8): warning C4003: not enough arguments for function-like macro invocation 'ADD'

GCC(带有 -E 标志)简单地按预期输出

int foo() {
return (1 + 2);
}

我看了一下C99标准。但我仍然不确定哪个编译器做得对?

我希望有人能帮我澄清一下。

最佳答案

我认为 gcc 是正确的。尽管 C11 6.10.3/12 将 COMB 的调用描述为具有两个参数(ADD1,2),但一旦 COMB 被扩展生成的 token 序列是 ADD ( 1 , 2 ),而6.10.3.4/1则明确将第一次替换的结果重新扫描为预处理 token 序列。上一步中由多个标记组成的参数不会以某种方式粘附到单个标记中以进行重新扫描。

6.10.3.4/1:

After all parameters in the replacement list have been substituted and # and ## processing has taken place, all placemarker preprocessing tokens are removed. The resulting preprocessing token sequence is then rescanned, along with all subsequent preprocessing tokens of the source file, for more macro names to replace

关于c - Variadic 宏扩展出错了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59212551/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com