Path: utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!cs.utexas.edu!sm.unisys.com!oberon!stromboli.usc.edu!english From: english@stromboli.usc.edu (Joe English) Newsgroups: comp.lang.c++ Subject: Re: Improved switch statement (was Re: goodbye cpp ???) Summary: Not really an improvement... Message-ID: <14104@oberon.USC.EDU> Date: 16 Dec 88 23:01:45 GMT References: <6590072@hplsla.HP.COM> <1106@etive.ed.ac.uk> <33528@bbn.COM> <1907@ogccse.ogc.edu> <574@redsox.UUCP> Sender: news@oberon.USC.EDU Reply-To: english@stromboli.usc.edu (Joe English) Distribution: na Organization: University of Southern California, Los Angeles, CA Lines: 36 In article <574@redsox.UUCP> campbell@redsox.UUCP (Larry Campbell) writes: >In article <1907@ogccse.ogc.edu> wm@ogccse.UUCP (Wm Leler) writes: >}I don't think you could extend switch statements to work on strings, >}since there is some ambiguity between whether you want to compare >}the strings by comparing their contents (using strcmp) or by comparing >}their pointers (a very reasonable thing to do). > >You leave that decision to the person who implemented == for the class. >I think it would be eminently sensible for the switch statement to operate >on any type that has an == operator defined; of course the case labels would >have to be the correct type (or coerced thereto). > >A common and useful idiom I learned in BLISS which is, unfortunately, not >possible in C or C++ today, is the following code fragment (transliterated >from BLISS into pseudo-C), which checks a number of boolean flags: > [code fragment using switch(), equivalent to if (flag1)...else if (flag2)... ] This is useful only as syntactic sugar: the switch statement in C/C++ does *not* compile into a series of if (expr == case1) ... else if (expr == case2) ... statements, as your suggestion implies. Instead, (in all the implementations I've seen, anyway) it does a vectored jump, or if the (case_i) values are not close together (e.g., case 10: ... case 100:... case 1000:...) into a table lookup with a vectored jump, both of which are much more efficient than a series of if () tests. The switch statement you suggest would (of necessity) compile into a bunch of if()s, which is not really what switch was designed to do... /|/| "How do you convince your dermatologist -----< | | that you're being sexually responsible? O \|\| english%lipari@oberon.usc.edu