Why does the C preprocessor consider enum values as equal?

后端 未结 7 1331
南方客
南方客 2020-12-08 12:48

Why does the std::cout line in the following code run even though A and B are different?

#include 

en         


        
7条回答
  •  被撕碎了的回忆
    2020-12-08 13:26

    There are no macros called A or B, so on your #if line, A and B get replaced by 0, so you actually have:

    enum T { A = 1, B = 2 };
    
    int main() {
    #if (0 == 0)
        std::cout << A << B;
    #endif
    }
    

    The preprocessor runs before the compiler knows anything about your enum. The preprocessor only knows about macros (#define).

提交回复
热议问题