473,561 Members | 3,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression"

C/C++ language proposal:
Change the 'case expression' from "integral constant-expression" to "integral expression"

The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) [see also 5.19]:

case constant-expression :

I propose that the case expression of the switch statement
be changed from "integral constant-expression" to "integral expression".
This opens up many new possibilities since then also function calls
would be permitted in the case expression.
The old case case would continue to function since
it is a subset of the new case case.

Example usage:

//...
int f()
{
//...
return BLA1;
}

int g()
{
//...
return BLA2;
}

int h()
{
//...
return BLA3;
}

int y, x = f();
switch (x)
{
case 123 : y = g(); break;
case g() : y = 456; break; // using new case feature, ie. func-call
case h() : y = 789; break; // ditto
default : y = -1; break;
}

Oct 23 '08 #1
56 6685
On October 23, 2008 14:17, in comp.lang.c, Adem (fo***********@ alicewho.com)
wrote:
C/C++ language proposal:
Change the 'case expression' from "integral constant-expression" to
"integral expression"

The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) [see also 5.19]:
[snip]

Sorry, but your proposal is off-topic for the comp.lang.c newsgroup. I
suspect that it is also off-topic for comp.lang.c++.

As C and C++ are two separate languages, each defined by their own
standards, I suggest that you break your "C/C++" proposal into a "C
proposal" and a separate "C++ proposal", and submit each to their
respective standards bodies.

For C, you /might/ post your proposal to the comp.std.c newsgroup, as that
is the group that discusses the ramifications of and enhancements to the C
language standard.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Oct 23 '08 #2
Adem wrote:
C/C++ language proposal:
Change the 'case expression' from "integral constant-expression" to "integral expression"

The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) [see also 5.19]:

case constant-expression :

I propose that the case expression of the switch statement
be changed from "integral constant-expression" to "integral expression".
switch (rand()) {
case rand(): puts ("What's"); break;
case rand(): puts ("wrong"); break;
case rand(): puts ("with"); break;
case rand(): puts ("this"); break;
case rand(): puts ("picture?") ; break;
}

--
Er*********@sun .com
Oct 23 '08 #3
Eric Sosman <Er*********@su n.comwrites:
Adem wrote:
>C/C++ language proposal: Change the 'case expression' from
"integral constant-expression" to "integral expression"
The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) [see also 5.19]:
case constant-expression : I propose that the case expression
of the switch statement
be changed from "integral constant-expression" to "integral expression".

switch (rand()) {
case rand(): puts ("What's"); break;
case rand(): puts ("wrong"); break;
case rand(): puts ("with"); break;
case rand(): puts ("this"); break;
case rand(): puts ("picture?") ; break;
}
Well, it *could* be defined to be equivalent to the following:

{
const int __tmp = rand();
if (__tmp == rand() puts("What's");
else if (__tmp == rand() puts("wrong");
else if (__tmp == rand() puts("with");
else if (__tmp == rand() puts("this");
else if (__tmp == rand() puts("picture?" );
}

That's intended to be equivalent code just for this example, not a
definition; other transformations would be required when one case
falls through to the next. And I refuse to think about how this would
affect Duff's Device.

The point of C's switch statement is that it can be implemented
efficiently as a jump table, in contrast to an if/else chain where the
conditions have to be evaluated sequentially.

If you want to do something like a switch statement, but where the
case values aren't constant, you can always just write an if/else
chain. If you want to support this kind of thing in the language, so
that you only have to specify the LHS of the comparison once, I think
I'd prefer to introduce a new construct rather than adding this
functionality to the existing switch/case construct. With this
proposal, changing a single expression from a constant to a
non-constant could have substantial effects on the generated code, and
depending on how it's defined, subtle effects on the code's behavior.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 23 '08 #4
Lew Pitcher wrote:
Adem (fo***********@ alicewho.com) wrote:
>C/C++ language proposal:
Change the 'case expression' from "integral constant-expression"
to "integral expression"

The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) [see also 5.19]:

[snip]

Sorry, but your proposal is off-topic for the comp.lang.c
newsgroup. I suspect that it is also off-topic for comp.lang.c++.

As C and C++ are two separate languages, each defined by their
own standards, I suggest that you break your "C/C++" proposal
into a "C proposal" and a separate "C++ proposal", and submit
each to their respective standards bodies.

For C, you /might/ post your proposal to the comp.std.c newsgroup,
as that is the group that discusses the ramifications of and
enhancements to the C language standard.
In addition, it is a BAD IDEA. Eliminating the constant provision
prevents making efficient transfer tables. It even prevents
discovering that such tables are feasible (or not).

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Oct 23 '08 #5
In article <49************ **@yahoo.com>, cb********@main eline.net wrote:
Lew Pitcher wrote:
Adem (fo***********@ alicewho.com) wrote:
C/C++ language proposal:
Change the 'case expression' from "integral constant-expression"
to "integral expression"
[...]
In addition, it is a BAD IDEA. Eliminating the constant provision
prevents making efficient transfer tables. It even prevents
discovering that such tables are feasible (or not).
It doesn't eliminate the privision, it just makes it more difficult. In
effect, optimizing

switch ( f() )
{
case 1: { /* A */ } break;
case 2: { /* B */ } break;
case 3: { /* C */ } break;
case 4: { /* D */ } break;
}

becomes similar optimizing

int const x = f();
if ( x == 1 ) { /* A */ }
else if ( x == 2 ) { /* B */ }
else if ( x == 3 ) { /* C */ }
else if ( x == 4 ) { /* D */ }

since both involve an unchanging value and constant values being compared
to. A compiler could convert both into a jump table or binary search.
Oct 23 '08 #6
On Oct 24, 3:12 am, Keith Thompson <ks...@mib.orgw rote:
Eric Sosman <Eric.Sos...@su n.comwrites:
Adem wrote:
C/C++ language proposal: Change the 'case expression' from
"integral constant-expression" to "integral expression"
The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) [see also 5.19]:
case constant-expression : I propose that the case expression
of the switch statement
be changed from "integral constant-expression" to "integral expression".
switch (rand()) {
case rand(): puts ("What's"); break;
case rand(): puts ("wrong"); break;
case rand(): puts ("with"); break;
case rand(): puts ("this"); break;
case rand(): puts ("picture?") ; break;
}
Wonderful!
>
Well, it *could* be defined to be equivalent to the following:

{
const int __tmp = rand();
if (__tmp == rand() puts("What's");
else if (__tmp == rand() puts("wrong");
else if (__tmp == rand() puts("with");
else if (__tmp == rand() puts("this");
else if (__tmp == rand() puts("picture?" );

}
If rand() generates equal random numbers frequently, is it meaningful?
Oct 24 '08 #7
lo************* **@gmail.c0m wrote:
On Oct 24, 3:12 am, Keith Thompson <ks...@mib.orgw rote:
....
>Well, it *could* be defined to be equivalent to the following:

{
const int __tmp = rand();
if (__tmp == rand() puts("What's");
else if (__tmp == rand() puts("wrong");
else if (__tmp == rand() puts("with");
else if (__tmp == rand() puts("this");
else if (__tmp == rand() puts("picture?" );

}

If rand() generates equal random numbers frequently, is it meaningful?
That code neither makes nor implies any assumptions about how frequently
rand() generates random numbers.
Oct 24 '08 #8

"Eric Sosman" <Er*********@su n.comwrote in message
news:1224788466 .760836@news1nw k...
Adem wrote:
>C/C++ language proposal: Change the 'case expression' from "integral
constant-expression" to "integral expression"

The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) [see also 5.19]:

case constant-expression : I propose that the case expression of
the switch statement
be changed from "integral constant-expression" to "integral expression".

switch (rand()) {
case rand(): puts ("What's"); break;
case rand(): puts ("wrong"); break;
case rand(): puts ("with"); break;
case rand(): puts ("this"); break;
case rand(): puts ("picture?") ; break;
}
What's wrong with it that you're just injecting garbage. You can use the
same rand() trick with any statement:

for (i=rand(); i<=rand(); i=rand())...

But there's nothing really wrong with the OP's proposal, except it should be
a new statement type to keep the switch semantics clean. Then, it could also
use non-integer types.

C likes it's rather austere syntax however so I doubt this change will ever
be made.

--
Bartc

Oct 24 '08 #9
Bartc wrote:
>
"Eric Sosman" <Er*********@su n.comwrote in message
news:1224788466 .760836@news1nw k...
>Adem wrote:
>>C/C++ language proposal: Change the 'case expression' from "integral
constant-expression" to "integral expression"

The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) [see also 5.19]:

case constant-expression : I propose that the case expression
of the switch statement
be changed from "integral constant-expression" to "integral expression".

switch (rand()) {
case rand(): puts ("What's"); break;
case rand(): puts ("wrong"); break;
case rand(): puts ("with"); break;
case rand(): puts ("this"); break;
case rand(): puts ("picture?") ; break;
}

What's wrong with it that you're just injecting garbage. You can use the
same rand() trick with any statement:

for (i=rand(); i<=rand(); i=rand())...

But there's nothing really wrong with the OP's proposal, except it
should be a new statement type to keep the switch semantics clean. Then,
it could also use non-integer types.
There is something wrong. Try to compile this example, and I hope you
figure whats wrong with his proposal :

int main()
{
int a = 3;
switch( a )
{
case 3:
a = 1;
break;
case 3:
a = 2;
break;
case 3:
a = 3;
break;
default:
a = 4;
}
}
Oct 24 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
12345
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
87
5128
by: ziliath | last post by:
I recently tried out the Google "top coder" contest, as a C++ coder. I noticed immediately that they expected me to know STL. To which I say, what the fuck?! I may be missing something, but at what point when learning C++ was I supposed to have picked up STL? I ask because at every job I've had, and every job interview for that matter,...
2
4734
by: Susan Baker | last post by:
Hi, I got this error msg whilst building some classes. It is realatively asy to fix. But I just wondered, does anyone know the technical reason why one can't initialiaze a const static non-integral data member in a class?
53
4519
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document, and at the end there is even pure mindstorming text left in, but already I think it can be very useful. <url:...
9
6197
by: Xiangliang Meng | last post by:
Hi, all. I see a very strange fragment code today. Uint32 enum { a = 100; b = 200; }; NOTE: Uint32 is defined to be 'unsigned' in other source files.
86
3872
by: Randy Yates | last post by:
In Harbison and Steele's text (fourth edition, p.111) it is stated, The C language does not specify the range of integers that the integral types will represent, except ot say that type int may not be smaller than short and long may not be smaller than int. They go on to say, Many implementations represent characters in 8 bits, type...
48
3329
by: Frederick Gotham | last post by:
The "toupper" function takes an int as an argument. That's not too irrational given that a character literal is of type "int" in C. (Although why it isn't of type "char" escapes me... ) The "toupper" function imposes a further constrait in that the value passed to it must be representable as a unsigned char. (If C does not require all...
3
9436
by: _Christopher\(M2M\) | last post by:
Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600) How do I get rid of "Warning C4307: '-' : integral constant overflow" from the following macro? #define d_ReturnMax(I_iTypeSize)\ ( (int64(1)<<((I_iTypeSize*8)-1))-1) There NO warnings when done as a function: int64 d_ReturnMax(int64 I_iTypeSize) { return...
1
6588
by: Claire | last post by:
"Comparison to integral constant is useless; the constant is outside the range of type 'int'" How can I fix this compiler warning, please, for the following pseudocode? int m_nError; m_nError = somedriverapi.DoSomething(); if (m_nError != 0x8010002F) foobar; << compiler warning here thankyou Claire
36
3775
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used to read text or handle arrays of arbitrary length. I don't have the expertise in C of many folks here so I feel like I'm offering a small furry...
0
7570
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8088
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7930
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5472
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5187
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3617
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2068
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1181
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.