View Full Version : adding to C++

01-05-2003, 11:42 PM
how would you go about officially adding to the C++ language.

for example...

I am sure we all know about the select case function in BASIC. Now, C++ surpasses BASIC by far in all aspects, except for this one, I believe. And of course it is not essential, but it is nice.

I'm sorry, but C++'s switch statement just pales compared to BASIC's powerful select case. The reason for that is because switch requires constant values, while select case does not.

Now of course, there are ways you could build a library of your own to make some sort of make-shift select case for C++ if you wanted to, but would it not be possible to just go to ANSI and have the standard C++ switch statement changed to make it able to support things other than constants?

I liked the ability in basic to be able to do things like:

PRINT this
CASE >= 35
PRINT that
PRINT the_other_thing

Now, of course you couldn't do that with the switch statement, it just is not possible, because it only accepts constants. Yes, you could do it using if statements, which is what we all do, but I just like the power of select case, and would like it to be equaled by C++'s switch.

01-06-2003, 12:13 AM
I wouldn't try to change the langauge until I had a complete understanding of the current standard for the langauge, I would want to know what code the changes would potentially break, and generally have a very deep understanding of it before suggesting improvements. Reaching that level however, is probably more effort than it's worth.

Having switch support just constants (and furthermore, not breaking by default), is an issue with speed. It enables the compiler to make optimizations.

01-06-2003, 12:51 AM
how would you implement it into your compiler?

01-06-2003, 01:18 AM
You have to send your suggestion
to the ansi committe they have setup. I'm pretty sure they will
only consider new features if there are implementations that
have it and systems built using it.

01-06-2003, 02:01 AM
switch( x >= 35 ? 2 : x > 21 ? 1 : 0 ){

case 2: cout << this; break;
case 1: cout << that; break;
default: cout << the_other_thing;


or I would just do:

cout << x >= 35 ? this : x > 21 : that : the_other_thing ;

01-06-2003, 08:43 AM
there's a reason they have to be constant.

case 1:
case 2:
case 2:

does not compile because there should be only one case for 2. If it's not a constant then they cases could be set at run time which could produce this situation.

if you want non constants there's a real simple C way to do it. It's called if-else if-else

01-06-2003, 12:34 PM
Yes, you could do it using if statements, which is what we all do, but I just like the power of select case, and would like it to be equaled by C++'s switch.

Yes, most programmers would use if-statements. But is that weaker than select case?

If you need code for the example you gave, one would use if-statements, in other cases one would use switch.

In my opinion if-statements are very useful when it comes to ranges and switch is very useful when it comes to options.

01-06-2003, 01:02 PM
You have to submit a proposal to the standards comittee. That proposal must satisfy several different criteria. You can find out how to do this in a back issue of CUJ at www.cuj.com (http://www.cuj.com) in matt austerns column.