safe enum
safe enum
for anyone who doesn't know, this "-Werror=switch-enum" compiler option make the compiler throw an error if all of the enum values aren't explicitly handled in a "switch" statement
cpp
enum class colors { blue, red, purple, } void func(colors c) { switch(c) { case colors::blue: // do something break; case colors::red: // do something break; default: // do something break; } } int main() { func(colors::blue); }
this code doesn't compile on clang and gcc with the option "-Werror=switch-enum" because the "colors::purple" isn't explicitly handled. be aware that it doesn't throw a compiler error for "if" statements if one of the values isn't handled