C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Switch-statement

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
ConfusedGuy
Guest





PostPosted: Tue Aug 15, 2006 11:09 pm    Post subject: Switch-statement Reply with quote



{ to learn the reasons why certain things are the way they are in C++
you might consider asking in comp.std.c++, as well. also, commonly
recommended reading on that is "Design and Evolution of C++". -mod }

hi,

i was wondering why it is not possible to use variables for the switch
statements. i am more interessted in the technical reason for this and what
are the benefits compared to if-statements (except easier
read/writeabtility).

thanks.

ConfusedGuy




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Tutone
Guest





PostPosted: Wed Aug 16, 2006 12:18 am    Post subject: Re: Switch-statement Reply with quote



ConfusedGuy wrote:

Quote:
i was wondering why it is not possible to use variables for the switch
statements. i am more interessted in the technical reason for this and
what
are the benefits compared to if-statements (except easier
read/writeabtility).

switch statements are very low-level constructs inherited from C, and
therefore their syntax, strengths, and shortcomings come from C.

The C language includes switch statements to make it easier for a
compiler to implement a look-up table optimization. Most compilers
will turn certain switch statements into a look-up table that offers
constant time branch selection, whereas few if any compilers will
implement this optimization for a series of nested if-else statements.
Without the optimization, branch selection will take longer the further
down the selected branch is in the list. (Note that there is no
_requirement_ in the language that the compiler turn a switch statement
into a look-up table, nor is there anything in the language prohibiting
a compiler from converting an appropriate series of nested if-else
statements into a look-up table. It's a quality-of-implementation
issue, and a switch statement just makes its much easier for compiler
writers to implement the optimization.)

If the language permitted variables in "case" portion of a switch
statement, the compiler could not turn that switch statement into a
look-up table, depriving the construct of any usefulness other than
syntactic sugar (what you call "easier read/writeability").

Also note that in C++, in many instances it is more idiomatic (and
potentially more efficient, depending on how the compiler implements
the switch statement) to use virtual functions where one might use a
switch statement in C.

Best regards,

Tom


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Bo Persson
Guest





PostPosted: Thu Aug 17, 2006 3:34 am    Post subject: Re: Switch-statement Reply with quote



"Seungbeom Kim" <musiphil (AT) bawi (DOT) org> skrev i meddelandet
news:ebumje$6q$1 (AT) news (DOT) Stanford.EDU...
Quote:
Thomas Tutone wrote:

If the language permitted variables in "case" portion of a switch
statement, the compiler could not turn that switch statement into a
look-up table, depriving the construct of any usefulness other than
syntactic sugar (what you call "easier read/writeability").

"Easier readability/writability" alone can be a good argument, can't
it?
A quality implementation could translate switch statements only with
constant case labels into table look-ups, and others into if-else
chains,
which would be perfectly valid and also useful at the same time.
I don't see why "in order to allow table look-ups" can justify the
banning of non-constant case labels, even when table look-up is not
a
requirement.


You will also get the additional trouble of defining a definite order,
when the labels are non-unique at run-time.


Bo Persson



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Tutone
Guest





PostPosted: Thu Aug 17, 2006 6:38 am    Post subject: Re: Switch-statement Reply with quote

Seungbeom Kim wrote:

Quote:
Thomas Tutone wrote:

If the language permitted variables in "case" portion of a switch
statement, the compiler could not turn that switch statement into a
look-up table, depriving the construct of any usefulness other than
syntactic sugar (what you call "easier read/writeability").

"Easier readability/writability" alone can be a good argument, can't it?

Yes, it can. But remember that the OP asked: "what are the benefits
compared to if-statements (except easier read/writeabtility)." Note
the OP's parenthetical - I was responding as asked.

Quote:
A quality implementation could translate switch statements only with
constant case labels into table look-ups, and others into if-else chains,
which would be perfectly valid and also useful at the same time.
I don't see why "in order to allow table look-ups" can justify the
banning of non-constant case labels, even when table look-up is not a
requirement.

A reasonable argument. But unless the same change were made to C at
the same time, this would introduce yet another incompatibility between
C and C++, which - all other things being equal - many would view as a
result to be avoided.

Best regards,

Tom


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.