C++11 mixing enum class and unsigned int in switch case will not compile

The whole purpose of the enum class was so that its members couldn't be compared directly to ints, ostensibly improving the type safety of C++11 relative to C++03. Remove class from enum class and this will compile.

To quote Lord Bjarne:

(An) enum class (a scoped enumeration) is an enum where the enumerators are within scope of the enumeration and no implicit conversions to other types are provided.


You can simply use such a syntax:

enum class Test { foo = 1, bar = 2 };
int main()
{
  int k = 1;
  switch (static_cast<Test>(k)) {
    case Test::foo: /*action here*/ break;
  }
}

An alternative that keeps using enum class is to add a new field that represents a value of 2 to myEnum. Then you can change unsigned int v to myEnum v.

enum class myEnum : unsigned int
{
    foo = 2,
    bar = 3
};

int main() {
    myEnum v = myEnum::foo;
    switch(v)
    {
        case myEnum::foo:
        break;

        case myEnum::bar:
        break;
    }
}