🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2806

Stuff C++ needs

#2806
From: Kevlin A P Henney
Date: Sun, 03 Dec 1995 22:13
58 lines
2479 bytes
In article <49r5vc$pk2@news.aloha.com> jching@aloha.com "Jimen Ching" writes:

 [standard switch statement]

 > This is valid for C and C++.  So for the above premise of C++, I should
 > also be able to do the following as well:
 > 
 > class internal_type
 >         {
 >         friend bool operator==(const internal_type &lhs, const internal_type
 >  &rhs)
 >                 { return lhs.data == rhs.data; }
 > public:
 >         internal_type(char c = '0') : data(c) {}
 > private:
 >         char data;
 >         };
 > 
 > internal_type const a('a');
 > internal_type const b('b');
 > internal_type const c('c');
 > 
 > f(internal_type &it)
 >         {
 >         switch (it)
 >                 {
 >                 case a: /* do something... */ break;
 >                 case b: /* do something... */ break;
 >                 default: /* do something... */ break;
 >                 }
 >         }

Why? Given that an important feature of a switch statement is that the
target labels are unique, how do you intend to determine this at compile
time for runtime expressions? Don't forget that C/C++ builtins don't allow
you to runaway w/ the show as you've suggested: floats and pointers are not
switchable [Sun used to support switching on floats, but pointers could be
kind of amusing, so perhaps next 1st April...].

 > If C++ lived up to its promise, why can't I do the above?

It lives up to its promises because it _doesn't_ do the above ;->

[example omitted]

Sounds like enums to me -- you've got all the language you need to do the job.
_______________________________________________________________________

  Kevlin A P Henney        2sdg Ltd        kevlin@two-sdg.demon.co.uk
  "What happens to the hole when the cheese is gone?"  Bertolt Brecht
_______________________________________________________________________


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <1231@two-sdg.demon.co.uk>
Path: arc1-ca.pugleaf.net!howland.reston.ans.net!agate!agate!Matt

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!howland.reston.ans.net!agate!agate!Matt
From: Kevlin@two-sdg.demon.co.uk (Kevlin A P Henney)
Newsgroups: comp.std.c++
Subject: Stuff C++ needs
Date: 03 Dec 1995 22:13:17 GMT
Organization: 2sdg Ltd
Approved: Matt@Austern
Message-ID: <1231@two-sdg.demon.co.uk>
Reply-To: kevlin@two-sdg.demon.co.uk
NNTP-Posting-Host: austern.hip.berkeley.edu