In C++ it's allowed to have values of an enumeration type which don't correspond to any enumerator so long as they are in range:
enum class E { X = 1, Y = 2 };
E z = static_cast<E>(0);
Is it possible to switch over such a value? That is,
void f(E a) {
switch(a) {
case E::X: // something
case E::Y: // something else
// somehow check if it's the same as z
default: // something else again
}
}
I expect it isn't, but wanted to confirm, and will be quite happy if it is.
The unsatisfactory options I know:
switch(static_cast<int>(a)) {
case 1: // something
case 2: // something else
case 0: // the z case
default: // something else again
}
or
if (a == E::X) {
// something
} else if (a == E::Y) {
// something else
} else if (static_cast<int>(a) == 0)
// the z case
} else {
// something else again
}