-Werror=switch
Using this, you can enforce that a switch statement over an enum value is complete (covers all cases) simply by omitting the default case.
-Werror=switch
Using this, you can enforce that a switch statement over an enum value is complete (covers all cases) simply by omitting the default case.
This is not about the coding police coming to get you but preventing you from shooting yourself into your feet when you extend the enumeration. Quite often you want to cover all cases and write code that signals an error if the default branch is called. CL even has a special version of the CASE macro (CL's "switch") called ECASE[1] with the only difference being that ECASE signals an error on unhandled cases.
[1] http://www.lispworks.com/documentation/HyperSpec/Body/m_case...
This.
And not necessarily yourself, but whomever maintains the software after you.
Of course this doesn't prevent someone from adding a "default" label in some future iteration.
There are exceptions to this "some cases are no-ops" thing. But I think it's the minority of switches I've seen.
From that perspective it does seem like the coding police angle. It's reminiscent of certain things in Java, like making a bunch of implicit conversions into errors, or C#, which forbids implicit fall-through on a switch. Those too are for safety and maintainability. They also make a lot of constructs more ugly. My question is: is it worth it? In this case (unintended pun) I don't think so.
Then you use:
default: break;
It's a small price to pay for the consistency checks elsewhere when you don't have a default.