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

  1. Xref: sparky comp.lang.c++:16665 comp.std.c++:1606
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!pipex!warwick!bham!bhamvx!mccauleyba
  4. From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
  5. Subject: Re: Proposal - enhancement for switch statement.
  6. Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
  7. Message-ID: <1992Nov20.195059.1@vax1.bham.ac.uk>
  8. Date: Fri, 20 Nov 1992 19:50:59 GMT
  9. Lines: 59
  10. References: <1992Nov16.004558.9855@muddcs.claremont.edu> <1992Nov16.213015.1@vax1.bham.ac.uk> <1992Nov19.163944.19935@ucc.su.OZ.AU>
  11. Organization: University of Birmingham
  12.  
  13. In article <1992Nov19.163944.19935@ucc.su.OZ.AU>, maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  14. > In article <1992Nov16.213015.1@vax1.bham.ac.uk> mccauleyba@vax1.bham.ac.uk (Brian McCauley) writes:
  15. >>Now (as promised) why ranges would be unnecessary (following the "if you
  16. >>can do it with a class then don't extend the language" rule ;-) )
  17. >>
  18. >>template <class T> class Range {
  19. >>  T top,bottom; 
  20. >>public:
  21. >>  Range(t,b) : top(t),bottom(b) {} 
  22. >>  int operator==(T t) { return bottom <= t && top >= t; }
  23. >>}
  24. >>template <class T> inline operator == (T& t,Range<T>& r) {return r == t;}
  25. >>
  26. >>switch (i) {
  27. >>  case Range<char>('0','9') : case '-' : {/*...*/} break;
  28. >>}
  29. >     This is illegal. 
  30. >     The 'argument' of a case must be a constant expression.
  31. >     switch is a primitive operation, the compiler must be able to
  32. >     check for duplicate cases.
  33.  
  34. Of course it's illegal! I do wish people would pay attension to the
  35. subject of the thread. What is being to discussed is the merit of
  36. allowing the switch command to accept an argument of any type and
  37. resolve cases using the appropriate == operator. Since there's no
  38. such thing as a compile-time class object constant this implies that
  39. the case clause would have to be able to take an expression if
  40. this proposal were accepted. It would also mean that switch was not
  41. a primative operation (unless it happened to obey C rules in which
  42. case it would optimize back to a primative operation). The ability for
  43. the compiler to detect duplicate cases would also be lost when varaible
  44. expressions where use in case clauses (or when the == operator was not
  45. inline). The complier does not _need_ to be able to detect duplicate
  46. cases if its not trying to make a jump table and it couldn't in these
  47. cases anyhow.
  48.  
  49. The range issue was introduced as an amendment and I was demonstrating
  50. that it was redundant given the implications of the original proposal.
  51.  
  52. IMHO...
  53.  
  54. On balance the extension of switch to operate on any class is probably
  55. unecessary and the extension to case to take ranges is therefore a 
  56. good idea. 
  57.  
  58. Removing the need for break between case clauses is a bad idea
  59. - the need to avoid fallthrough is better handled by a complier warning.
  60.  
  61. Adding a new syntax (or worse still token) to the language should not
  62. be done lightly.
  63. -- 
  64.     \\   ( )    No Bullshit!     |  Email: B.A.McCauley@bham.ac.uk   
  65.  .  _\\__[oo        from         |  Voice: +44 21 471 3789 (home)
  66. .__/  \\ /\@    /~)  /~[   /\/[  |    Fax: +44 21 625 2175 (work)
  67. .  l___\\      /~~) /~~[  /   [  |  Snail: 197 Harborne Lane,
  68.  # ll  l\\    ~~~~ ~   ~ ~    ~  |         Birmingham, B29 6SS, UK
  69. ###LL  LL\\   (Brian McCauley)   |   ICBM: 52.5N 1.9W
  70.