Enum classes are supposed to be strong enums in the sense that they don't implicitly convert to and from int
. For instance:
enum class EC { a, b };
However, when switching over such a "strong enum":
int sw(EC ec) {
switch (ec) {
case EC::a: return 0;
case EC::b: return 1;
}
}
gcc -Wreturn-type
-wants me to add a default
clause to the switch even though all legal enum values are covered:
warning: control reaches end of non-void function [-Wreturn-type]
In an old (non-class) enum, this makes sense, because any int
could have been accidentally converted to EC
. But I had (apparently wrongly) assumed that assigning an invalid enum member to an enum class was UB.
How can I use truly strong enum classes where the compiler realises that functions like sw
cover all possible paths? Of course I could just add a default:
branch that I know will never be triggered, but I want to make sure that adding more members to EC
in the future will trigger a warning in the switch.