Why does the C preprocessor consider enum values as equal?

后端 未结 7 1325
南方客
南方客 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:40

    Other answers explain why what you're trying doesn't work; for an alternative, I'd probably go with:

    #define RGB 1
    #define GREY 2
    #define CMYK 3
    #define MODE RGB
    
    #if MODE == RGB
        //RGB-mode code
    #elif MODE == GREY
        //Greyscale code
    #elif MODE == CMYK
        //CMYK code
    #else
    #    error Undefined MODE
    #endif
    

    You might want prefixes on the RGB/GREY/CMYK if there's a danger of clashes with "real" source code.

提交回复
热议问题