home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!linac!att!cbnewsc!cbfsb!cbnewsg.cb.att.com!nh
- From: nh@cbnewsg.cb.att.com (nicholas.hounsome)
- Subject: Re: Proposal - enhancement for switch statement.
- Message-ID: <1992Nov16.093252.14843@cbfsb.cb.att.com>
- Sender: news@cbfsb.cb.att.com
- Organization: AT&T
- References: <BxsHtz.8qJ@sti.com>
- Date: Mon, 16 Nov 1992 09:32:52 GMT
- Lines: 57
-
- From article <BxsHtz.8qJ@sti.com>, by dgreen@sti.com (Dan R. Greening):
- > mcgrant@rascals.stanford.edu (Michael C. Grant) wrote:
- >>In article <1992Nov15.190830.11622@cssc-syd.tansu.com.au>
- >>pete@cssc-syd.tansu.com.au (Peter Alexander Merel) writes:
- >>> [switch on any type using == proposal]
- >>> Any chance?
- >>
- >>I doubt it... Even before C++ came along, switch statements could not
- >>handle floats, strings, structures, etc. Sure, a switch statement could
- >>be turned into a set of if-then-else statements, but it isn't because
- >>it usually is best turned into a jump table.
- >
- > Smart compilers should put it into a series of jump tables and do binary
- > search to identify the correct jump table.
- >
- > The obvious rhetorical question is "why not require SwitchType to have
- > a well-ordering relation using operator< and operator==?". They you could
- > at least do the binary search part instead of a sequence of "=="
- > operations.
- >
- > But the rhetoric is just that: I'd prefer to keep it simple, and leave
- > switch alone.
- >
- > The reason is pretty simple: you as the programmer know your data much
- > better than the compiler. If you are going to use expensive operations,
- > like a series of categorizing "==" operator calls, it seems like you
- > should tailor your "switch" to the data. The compiler can't do that.
- > --
- > ____
- > \ /Dan Greening Software Transformation 1601 Saratoga-Sunnyvale Rd, #100
- > \/dgreen@sti.com (408) 973-8081 x313 Cupertino, CA 95014
-
- Most switches I have seen are so small that the overhead of range checking
- and the possibility of having to have several tables make the whole thing a
- waste of time.
-
- I haven't checked what my sun compiler does but in the past when I checked
- output from other compilers I found that they just implemented switches as
- if-then-else anyway.
-
- I find that switch statements tend to be much larger than if-then-else -
- for some reason people seem less inclined to use subsidiary functions in switches than
- in if-then-else. I have no idea why this should be so.
-
- I find that having break for both loops and switches means that I have
- to create loop exit variables which I hate - I would much rather write
- ... if( c == 'q' ) break; than case 'q': quit = TRUE; break;
-
- In this day and age we should expect compilers to recogize a sequence
- of i-then-else which can be implemented as a jump table. The register
- qualifier is now universaly unnecessary - the switch statement should
- be as well.
-
- If you use classes extensively there is no need for switch because
- you cannot create jump tables which was its original raison d'etre.
-
- Nick Hounsome
-