c++ enum class integer not working for array subscript

c++, c++11

Solution

As enum classes are strongly typed I would expect this to work

On the contrary, that's exactly why it won't work. Scoped enumerations will not implicitly convert to the underlying type. Use `static_cast` instead.

MyObject a = arr[static_cast<int>(EnumClass::A)];

Problem

I have the following enum class: ``` enum class EnumClass : int { A = 0, B }; ``` Now I want to subscript with that enum type to an array: ``` MyObject arr[2]; . . . MyObject a = arr[EnumClass::A] MyObject b = arr[EnumClass::B] ``` Unfortunately I get the following error message: ``` array subscript is not an integer ``` As enum classes are strongly typed I would expect this to work.

Original source