Search code examples
c++templatesenumsbitset

bitset with scoped enums doesn't compile


I am trying to use a std::bitset with an enum but I am getting a compilation error saying

template argument 1 is invalid

Funny thing is that when I use any of the enumerated value without the enumeration scope it works fine.

Do you know why?

Below the code

   enum MyTypes {
     Alpha  = 1,
     Beta  = 2,
     Gamma = 3
   };


   std::bitset<MyTypes::Alpha> bitset_wrong; // It doesn't compile.
   std::bitset<Alpha >         bitset_good;  // It works.

Solution

  • It seems that you have an old compiler that does not support specifying qualified names with unscoped enumerators.

    Update your compiler.:)

    The code you showed is a valid code according to the C++ 2011 Standard.

    Here is a quote from the C++ Standard with an example (7.2 Enumeration declarations)

    11 Each enum-name and each unscoped enumerator is declared in the scope that immediately contains the enum-specifier. Each scoped enumerator is declared in the scope of the enumeration. These names obey the scope rules defined for all names in (3.3) and (3.4).

    [ Example:

    enum direction { left=’l’, right=’r’ };
    void g() {
    direction d; // OK
    d = left; // OK
    d = direction::right; // OK
    }