home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:16736 comp.std.c++:1608
- Newsgroups: comp.lang.c++,comp.std.c++
- Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
- From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
- Subject: Re: Proposal - enhancement for switch statement.
- Message-ID: <9232811.8739@mulga.cs.mu.OZ.AU>
- Sender: news@cs.mu.OZ.AU
- Organization: Computer Science, University of Melbourne, Australia
- References: <1992Nov16.004558.9855@muddcs.claremont.edu> <1992Nov16.213015.1@vax1.bham.ac.uk> <1992Nov19.163944.19935@ucc.su.OZ.AU> <1992Nov20.195059.1@vax1.bham.ac.uk>
- Date: Mon, 23 Nov 1992 00:14:18 GMT
- Lines: 53
-
- mccauleyba@vax1.bham.ac.uk (Brian McCauley) writes:
-
- >Removing the need for break between case clauses is a bad idea -
- >the need to avoid fallthrough is better handled by a compiler warning.
-
- The difficulty with this is that there has to be a standard way to
- prevent the compiler warning in cases where the fallthrough is
- deliberate rather than accidental. Because there isn't, the compiler
- warning would wrong more times than not, and so most compilers don't
- seem to include such a warning.
-
- Perhaps a good way to indicate that the fallthrough is deliberate would be to
- use an explicit goto.
-
- switch(x) {
- case 1:
- ...
- break;
- case 2:
- ...
- goto case_3: /* fall through */
- case_3:
- case 3:
- ...
- }
-
- Any half-decent compiler will of course be able to optimize out the goto.
- It is still irritating that you are not allowed to jump to case labels,
- so you have to invent an extra case_3 label. [ObLanguageExtension:
- allow goto's to jump to case labels (so long as the jump is unambiguous)].
-
- //----------------------------------------------------------------------//
-
- A technique that I have seen used is to
- #define when break; case
- #define otherwise break; default
- and then use
- switch(x) {
- when 1:
- ...
- when 2:
- ...
- otherwise:
- ...
- }
- If your compiler doesn't warn about fallthrough in switch statements,
- then this may well be worthwhile.
-
- --
- Fergus Henderson fjh@munta.cs.mu.OZ.AU
- This .signature virus is a self-referential statement that is true - but
- you will only be able to consistently believe it if you copy it to your own
- .signature file!
-