Do not define a default clause for enum in switch statement! Say in future, if another fruit type is added to the enum, default clause shall cause the C++ compiler not to warn you that you have not handled the case for the new fruit.
enum class Fruit { Apple, Orange }; Fruit fruit = ...; switch(fruit) { case Fruit::Apple: ... break; case Fruit::Orange: ... break; default: ... // <-- Do not ever define a default clause for enum! }
As a point of interest, first item under Bad Coding Practices is CWE-478: Missing Default Case in Switch Statement advises programmers to put default clause in their switch statement.
Leave a Reply