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