Thread View: comp.std.c++
2 messages
2 total messages
Started by Kevlin A P Henney
Sun, 03 Dec 1995 22:13
Stuff C++ needs
Author: Kevlin A P Henney
Date: Sun, 03 Dec 1995 22:13
Date: Sun, 03 Dec 1995 22:13
58 lines
2479 bytes
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. ]
Re: Stuff C++ needs
Author: Jimen Ching
Date: Wed, 06 Dec 1995 16:03
Date: Wed, 06 Dec 1995 16:03
32 lines
2381 bytes
2381 bytes
Kevlin A P Henney (Kevlin@two-sdg.demon.co.uk) wrote: >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? The labels are unique because they are intended to be used as constants, just like 'a' or 'z'. Just like you do not change 'a' at run time, you can not (or should not) change my constants either. I guess I should say I want to create my own constant 'literals', and be able to control the size. >Sounds like enums to me -- you've got all the language you need to do the job. I did use enums. I switched to a class because it allows me to control the size. The type I had in mind is used very often. Thus size and efficiency is very important. On many systems (especially 32 bit machiens), sizeof(enum) == 4. But, for my internal_type sizeof(internal_type) == 1. Thus, I can store four times as many data, and yet still have the operators for convience. The only problem was, I no long can use switch statements like I could with enums. I guess if I want speed, I have to use array indexing. But with large number of constants, the array will be large, so I still lose storage space. I just can't win. :) It would be great if the switch could detect that my internal type was actually a 'char' type and base the selection process on that. I'm not sure how complicated the compiler would be to do this. But I think it is possible. Maybe it should be an optional feature in C++. --jc -- Jimen Ching (WH6BRR) jching@aloha.com wh6brr@uhm.ampr.org [ 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. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads