#define many times without #undef,is it legal?

后端 未结 4 837
你的背包
你的背包 2020-12-21 20:22

For example, I define AA for three times, is it legal?:

#include
#define AA 10
#define AA 20
#define AA 30
int main() {
    printf(\"         


        
4条回答
  •  囚心锁ツ
    2020-12-21 20:54

    This is not legal in both C and C++.

    Quotes from draft C standard N1570:

    6.10.3 Macro replacement

    Constraints

    1 Tw o replacement lists are identical if and only if the preprocessing tokens in both have the same number, ordering, spelling, and white-space separation, where all white-space separations are considered identical.

    2 An identifier currently defined as an object-like macro shall not be redefined by another #define preprocessing directive unless the second definition is an object-like macro definition and the two replacement lists are identical. Likewise, an identifier currently defined as a function-like macro shall not be redefined by another #define preprocessing directive unless 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.

    Quotes from draft C++ standard N4582:

    16.3 Macro replacement [cpp.replace]

    1 Two replacement lists are identical if and only if the preprocessing tokens in both have the same number, ordering, spelling, and white-space separation, where all white-space separations are considered identical.

    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. 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.

提交回复
热议问题