home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16736 < prev    next >
Encoding:
Internet Message Format  |  1992-11-19  |  2.2 KB

  1. Xref: sparky comp.lang.c++:16736 comp.std.c++:1608
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  4. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  5. Subject: Re: Proposal - enhancement for switch statement.
  6. Message-ID: <9232811.8739@mulga.cs.mu.OZ.AU>
  7. Sender: news@cs.mu.OZ.AU
  8. Organization: Computer Science, University of Melbourne, Australia
  9. 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>
  10. Date: Mon, 23 Nov 1992 00:14:18 GMT
  11. Lines: 53
  12.  
  13. mccauleyba@vax1.bham.ac.uk (Brian McCauley) writes:
  14.  
  15. >Removing the need for break between case clauses is a bad idea -
  16. >the need to avoid fallthrough is better handled by a compiler warning.
  17.  
  18. The difficulty with this is that there has to be a standard way to
  19. prevent the compiler warning in cases where the fallthrough is
  20. deliberate rather than accidental. Because there isn't, the compiler
  21. warning would wrong more times than not, and so most compilers don't
  22. seem to include such a warning.
  23.  
  24. Perhaps a good way to indicate that the fallthrough is deliberate would be to
  25. use an explicit goto.
  26.  
  27.     switch(x) {
  28.         case 1:
  29.             ...
  30.             break;
  31.         case 2:
  32.             ...
  33.             goto case_3:    /* fall through */
  34.         case_3:
  35.         case 3:
  36.             ...
  37.     }
  38.  
  39. Any half-decent compiler will of course be able to optimize out the goto.
  40. It is still irritating that you are not allowed to jump to case labels,
  41. so you have to invent an extra case_3 label. [ObLanguageExtension:
  42. allow goto's to jump to case labels (so long as the jump is unambiguous)].
  43.  
  44. //----------------------------------------------------------------------//
  45.  
  46. A technique that I have seen used is to
  47.     #define       when        break; case
  48.     #define    otherwise    break; default
  49. and then use
  50.     switch(x) {
  51.         when 1:
  52.             ...
  53.         when 2:
  54.             ...
  55.         otherwise:
  56.             ...
  57.     }
  58. If your compiler doesn't warn about fallthrough in switch statements,
  59. then this may well be worthwhile.
  60.  
  61. -- 
  62. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  63. This .signature virus is a self-referential statement that is true - but 
  64. you will only be able to consistently believe it if you copy it to your own
  65. .signature file!
  66.