If I have an enum class like so
enum class Enum {
A = 1,
B = 2,
};
I was under the impression that the compiler guarantees that instances of Enum
must be either A
or B
. But I learned that the following is possible
auto instance = Enum{};
cout << (instance == Enum::A) << endl;
cout << (instance == Enum::B) << endl;
cout << static_cast<int>(instance) << endl;
And it prints 0 in all cases. Further I used to think having a switch-case like this is exhaustive
switch (instance) {
case Enum::A:
// do something
break;
case Enum::B:
// do something
break;
}
but apparently it's not, how can I handle a value-initialized instance above?