From 4405787379061609666 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,5be30c38f618a07b X-Google-Attributes: gidf78e5,public X-Google-ArrivalTime: 2002-03-21 10:05:04 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.uchicago.edu!newsfeed.cs.wisc.edu!newsfeed.mathworks.com!btnet-peer0!btnet!dispose.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail From: "Carl Daniel" Newsgroups: comp.std.c++ Subject: Re: Why switch w/ only integral types? Date: Thu, 21 Mar 2002 18:04:50 GMT Organization: Prodigy Internet http://www.prodigy.com Approved: Fergus Henderson , moderator of comp.std.c++ Message-ID: References: <6ee7d287.0203202005.63e0b7b3@posting.google.com> X-Trace: mail2news.demon.co.uk 1016733895 mail2news:29248 mail2news mail2news.demon.co.uk X-Complaints-To: abuse@demon.net X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Date: Thu, 21 Mar 2002 13:03:02 EST X-UserInfo1: F[OUBVOEQB^SSVDYLJJ\OPLI[B]NQHEKFA_J]Q]KEYUNDQUCCNSUAACY@L[ZX__HGFD]JBJNSFXTOOGA_VWY^_HG@FW_HUTHOH]TBPGCO\P^PLP^@[GLHUK@WLECKFVL^TYG[@RMWQXIWM[SDDYWNLG_G[_BWUCHFY_Y@AS@Q[B\APPF@DCZM_PG_VSCPQZM Lines: 55 Xref: archiver1.google.com comp.std.c++:10116 char IS an integral type, so you example is valid, assuming you fixed your switch statements to put the switch expression inside parentheses... (e.g. switch (i), switch (ch)). As for truly non-integral types, like std::string, I'd hazzard a guess that it's because switch was adopted as-is from C, and such things are illegal in C. They're illegal in C because the motivation for the switch statement was to crete a multiway branch that's more efficient than an if/else ladder. If non-integral types are allowed as case labels, the compiler will likely have little option but to generate an if/else ladder - which you could just have easily written yourself. Nevertheless, there are many languages which do permit arbirtray types in switch/case constructs, so I doubt there's any technical reason why C++ couldn't have been specified that way. -cd "Robb" wrote in message news:6ee7d287.0203202005.63e0b7b3@posting.google.com... > Why doesn't the standard allow switch to be used w/ non-integral types? > > ex: > int i; > char ch; > > //... > switch i{ // legal -- integral type > case 1: > //... > default: > //... > } > switch ch{ // illegal -- non-integral type > case 'A': > //... > default: > //... > } > > --- > [ comp.std.c++ is moderated. To submit articles, try just posting with ] > [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] > [ --- Please see the FAQ before posting. --- ] > [ FAQ: http://www.research.att.com/~austern/csc/faq.html ] > --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]