3 ms·
Assuming gcc or similar, -Wall will pick up on this. int main() { enum states { A, B, C } x = A; switch(x) { case A: break; cas
by rachelbythebay 14y ago
Assuming gcc or similar, -Wall will pick up on this.
int main() {
enum states { A, B, C } x = A;
switch(x) {
case A: break;
case B: break;
// C not handled on purpose
}
return 0;
}
$ g++ -Wall -Werror -o enum enum.cc
cc1plus: warnings being treated as errors
enum.cc: In function ‘int main()’:
enum.cc:3: warning: enumeration value ‘C’ not handled in switch
I like this behavior since I can deliberately leave out a default and make sure I always have every case handled explicitly.
- jacques_chester 14y agoThankyou for correcting me. I definitely need to find a better example. I'm ambivalent about whether to have default cases. The assert(FALSE) trick is still useful for catching missing breaks in the final case.