1

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. 
5
  • You need to compile with at least -std=c++11 in order to use C++11 features. Commented Apr 7, 2015 at 10:19
  • @PaulR: thank unfortunately I don't have it available so I need to remove the scope manually. Do you know the reason behind that error? I am very interested in knowing why compiler is complaining about that innocent template. Commented Apr 7, 2015 at 10:22
  • Does enum struct MyTypes{...} work as a scoped enumerator? I'm not sure if unscoped enumerators support qualified names by default, see en.cppreference.com/w/cpp/language/enum : Each enumerator becomes a named constant of the enumeration's type (that is, name), visible in the enclosing scope... Commented Apr 7, 2015 at 10:30
  • @AbruzzoForteeGentile: prior to C++11 you could only use a class name or namespace to qualify an identifier like this. MyTypes is neither, so it would be an error to try and use it as a qualifier. Commented Apr 7, 2015 at 10:40
  • Proper spelling of α is alpha. Commented Apr 7, 2015 at 10:45

1 Answer 1

2

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 } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Unfortunately I don't have C++11 that I can't use. Do you know the reason behind that error?
@Abruzzo Forte e Gentile AFAIK the old C++ 2003 Standard deos not allow to use qualified names for enumerators.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.