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

  1. Xref: sparky comp.lang.c++:16605 comp.std.c++:1602
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!munnari.oz.au!sol.deakin.OZ.AU!news.cs.uow.edu.au!cssc-syd.tansu.com.au!pete
  4. From: pete@cssc-syd.tansu.com.au (Peter Alexander Merel)
  5. Subject: Re: Proposal - enhancement for switch statement.
  6. Message-ID: <1992Nov19.213223.20140@cssc-syd.tansu.com.au>
  7. Organization: AOTC - CSSC
  8. References: <1992Nov16.004558.9855@muddcs.claremont.edu> <1992Nov16.213015.1@vax1.bham.ac.uk>
  9. Date: Thu, 19 Nov 1992 21:32:23 GMT
  10. Lines: 48
  11.  
  12. mccauleyba@vax1.bham.ac.uk (Brian McCauley) writes:
  13. >No. Please not another token in the parser.
  14.  
  15. Agreed. The C++ philosophy appears to be, only add a token if there's just
  16. no other way at all.
  17.  
  18. >The only problem is that if a class has both an `operator==(int)' and an
  19. >`operator int ()' defined inconsistantly the some existing switch 
  20. >statements would change their meaning.
  21.  
  22. I thought about this a little, and thanks to Mike Tiller for raising it
  23. privately. Mike raised an associated problem, that operator== doesn't
  24. have to return an int, but can return any damned thing it pleases ...
  25. so a really perverse operator== might return something that can't cast
  26. into an int.
  27.  
  28. But how about this: 
  29.  
  30. switch looks, in the first instance, for a way to cast the switch 
  31. expression and case constants into int. That should keep existing code 
  32. from breaking.
  33.  
  34. Failing to find a way to do this, switch looks for an operator== on the
  35. type returned from the switch expression with a distinctive signature, say
  36.  
  37. int const operator==(foo const &  bar) const
  38.  
  39. There's a precedent for this sort of usage in the copy constructor, and
  40. of course this way the case constants can have different types to match
  41. different overloaded operator==.
  42.  
  43. >switch(TRUE) {
  44. >  case expr1: { /* code */} break;
  45. >  case expr2: { /* code */} break;
  46. >  delfault: {/* code */}
  47. >}
  48.  
  49. >This is exactly equivalent to an if ladder but some people think it looks
  50. >nicer :-)
  51.  
  52. Looks way cool, but I think there's a little inconsistency with the previous
  53. switch - you've got the constant at the top, and we'd have to commute the
  54. operator== to get it to fly. Sounds messy to me.
  55.  
  56. -- 
  57. Internet: pete@cssc-syd.tansu.com.au   UUCP: {uunet,mcvax}!munnari!cssc-syd!pete
  58. Snail: 1/18-20 Orion Road, Lane Cove NSW 2066 Australia    Phone: +61 2 911 3130
  59. I am a big pond in a small fish.
  60.