简体   繁体   English

是否保证永远不会发生类似函数的宏参数名称替换?

[英]Is function-like macro argument name substitution guaranteed to never happen?

Consider the following code考虑以下代码

#define foo 38
#define F(foo) G(foo)
F(42);

I expect this code to fail to compile because after applying the first line second line should transform into #define F(38) G(38) which does not make any sense.我希望这段代码无法编译,因为在应用第一行后,第二行应该转换为#define F(38) G(38)这没有任何意义。 But on g++ it successfully compiles into G(42) , as if there was no first line at all.但是在 g++ 上它成功编译为G(42) ,好像根本没有第一行。 I was unable to find any mention of this behaviour in neither g++ docs nor c standard.我在 g++ docs 和 c 标准中都找不到任何提到这种行为的地方。 I know that code is ugly and should not be used in the first place, but I wonder if it has any guarantees to be portable.我知道代码很丑,一开始就不应该使用,但我想知道它是否有任何可移植性的保证。

C 2018 6.10 7 and C++ 2017 (draft n4659) 19 [cpp] 6 both say: C 2018 6.10 7 和 C++ 2017 (draft n4659) 19 [cpp] 6 都说:

The preprocessing tokens within a preprocessing directive are not subject to macro expansion unless otherwise stated.除非另有说明,预处理指令中的预处理标记不受宏扩展的影响。

For parameters of function-like macros, nothing is otherwise stated.对于类函数宏的参数,没有另外说明。 Therefore, the parameter names in the definition of a function-like macro are not replaced due to prior macro definitions.因此,类函数宏定义中的参数名称不会由于先前的宏定义而被替换。

(Examples of where macro expansion is otherwise stated include #if directives and #include directives.) (另外说明宏扩展的示例包括#if指令和#include指令。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM