5 ms·
My absolute favorite for modern C++ is -Werror=switch Using this, you can enforce that a switch statement over an enum value is complete (covers all cases) si
by minexew 9y ago
My absolute favorite for modern C++ is
-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.
- asveikau 9y agoYou're not the first person I've seen suggest that this condition is worth flagging, but I don't completely understand why. People who conform to such a style then go on to add a "default" case that is a no-op, which I guess is more explicit, but also feels a little tedious and verbose. Switch statements in C can do all sorts of funky things (vide: Duff's device) so I think omitting a case label for an enum value feels like a much lesser crime. What if you just don't have meaningful things to do for that enum value?
- chrisseaton 9y agoYou're arguing that there are cases where the warning doesn't apply... well that's why it's just a warning not an error. You're not disallowing using switch in other ways - you're just warning yourself when you do.
- asveikau 9y agoExcept that the comment I'm replying to says -Werror, meaning they would like to turn this warning into a build break. That seems a bit like language fascism to me. (And if it's not a build break, leaving the warning on stderr during a build is noise, which will cause you to ignore more important warnings.)
- deleted 9y ago[deleted]
- mnarayan01 9y agoConsider static type checking when adding a new value to the enum. If the switch has a default, then obviously you lose this, but it's nice for the others.
- minexew 9y agoI should have clarified more; you'd use this for switches where you need to always handle all cases. Then if a you add a new value to the enum but forget to update the switch, you get a compile-time error instead of a buggy program.
- asveikau 9y agoBut the flag affects all switches on enums.
- jlg23 9y ago> I think omitting a case label for an enum value feels like a much lesser crime. 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_.htm http://www.lispworks.com/documentation/HyperSpec/Body/m_case...
- balls187 9y ago> 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. 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.
- asveikau 9y agoI think you guys are missing something in my perplexed state at this construct. I'm not saying it out of lack of experience or because I disbelieve in the "going out of my way to make things more maintainable though verbosity" aspect. It's just that many times in practice I have rarely seen an honest switch statement that really needs case labels for all possible values. Much more common in my experience, 1 or more cases are no-ops. I think it's pointless to list out the no-ops, but I've seen people advocate just that. 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.
- Sean1708 9y ago
- hacking_again 9y ago> What if you just don't have meaningful things to do for that enum value? Then you use: default: break; It's a small price to pay for the consistency checks elsewhere when you don't have a default.