简体   繁体   English

C和C ++中的宏重新定义

[英]Macro redefinition in C and C++

I know that this code is valid both in C and C++: 我知道这段代码在C和C ++中都是有效的:

#define FOO 0
#define FOO 0

ISO/IEC 14882:2011 ISO / IEC 14882:2011

16.3 Macro replacement [cpp.replace] 16.3宏替换[cpp.replace]

2 An identifier currently defined as an object-like macro may be redefined by another #define preprocessing directive provided that the second definition is an object-like macro definition and the two replacement lists are identical, otherwise the program is ill-formed. 2当前定义为类似对象的宏的标识符可以由另一个#define预处理指令重新定义,前提是第二个定义是类似于对象的宏定义,并且两个替换列表相同,否则程序格式不正确。 Likewise, an identifier currently defined as a function-like macro may be redefined by another #define preprocessing directive provided that the second definition is a function-like macro definition that has the same number and spelling of parameters, and the two replacement lists are identical, otherwise the program is ill-formed. 同样,当前定义为类似函数的宏的标识符可以由另一个#define预处理指令重新定义,前提是第二个定义是具有相同数量和参数拼写的类函数宏定义,并且两个替换列表相同,否则该计划是不正确的。

But what about this code? 但是这段代码怎么样?

#define FOO 0
#define FOO FOO

Replacement lists are not identical at the start of preprocessing (only when the first replacement occurs). 替换列表在预处理开始时不相同(仅在第一次替换发生时)。

This is not allowed in either C or C++. C或C ++中不允许这样做。 The replacement list must be identical. 替换列表必须相同。 What you're talking about (after the first pass) is the result of processing the replacement list 1 , not the replacement list itself. 您所谈论的(第一次通过之后)是处理替换列表1的结果,而不是替换列表本身。 Since the replacement list itself is not identical, the code is not allowed. 由于替换列表本身不相同,因此不允许使用代码。


1 Or at least what the result would be if the preprocessor worked a particular way that happens to be different from how it actually does. 1或者至少如果预处理器以特定的方式工作,那么结果会与实际的方式不同。

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

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