Search code examples
c++enum-class

why does define conflict with enum class?


Here is a small code in which the conflict occurs. is there any way to get around this correctly?

#define DEBUG 0

enum class TypeEnum : int
{
    DEBUG = 0,
    INFO = 1
};

Solution

  • It's the nature of the preprocessor. Lines beginning with # are commands to the preprocessor. #define is a command that defines a text replacement, which will rewrite your code when preprocessed. In this case, all instances of DEBUG will be replaced with 0, so the code becomes:

    enum class TypeEnum : int
    {
        0 = 0,
        INFO = 1
    };
    

    Which, of course, doesn't make sense.