Search code examples
c++enumsmacrospreprocessor

Enum class scope resolution operator


When I tried to compile test.cc by g++ test.cc --std=c++14, I got the following error.

test.cc:5:26: error: expected unqualified-id before numeric constant
Colour colour = Colour::None;

test.cc

#include "state.h"
#include <X11/X.h>

int main(){
        Colour colour = Colour::None;
}

state.h

enum class Colour { None, Black, White };

And I found that by #include <X11/X.h>, None is defined as a constant

 #define None                 0L /* universal null resource or null atom */

What bothers me is that I've already used scope resolution operator, namely Colour::None, to specify which None I'm refering, but the error still occurred.


Solution

  • That's life I'm afraid, and it epitomises the reasons for macros being terrible.

    Once you've #included <X11/X.h>, the preprocessor will chew up your source code and the compiler will see

    Colour colour = Colour::0;
    

    which makes no sense.

    One fix would be to #undef None after including the file.