case statement ranges...

J

John Ratliff

Are case statement ranges standard C++ or a compiler extension?

ex:

switch (somevar) {
case 0 ... 9: method1(); break;
case 0xA: ... 0x23: method2(); break;
}

I thought they were standard, but I remember reading somewhere this was
a GNU extension. I want to remove them from my code if they are
non-standard.

Thanks,

--John Ratliff
 
V

Victor Bazarov

John said:
Are case statement ranges standard C++ or a compiler extension?

They are not standard.
ex:

switch (somevar) {
case 0 ... 9: method1(); break;
case 0xA: ... 0x23: method2(); break;
}

I thought they were standard, but I remember reading somewhere this
was a GNU extension. I want to remove them from my code if they are
non-standard.

You can always make sure by switching to "strict" mode, IIRC.

V
 
I

Ivan Novick

Are case statement ranges standard C++ or a compiler extension?

ex:

switch (somevar) {
case 0 ... 9: method1(); break;
case 0xA: ... 0x23: method2(); break;

}I thought they were standard, but I remember reading somewhere this was
a GNU extension. I want to remove them from my code if they are
non-standard.

I would say they are non-standard, i have never seen them in the C++
standard document.

Ivan
http://www.0x4849.net
 
D

Dave Rahardja

Are case statement ranges standard C++ or a compiler extension?

ex:

switch (somevar) {
case 0 ... 9: method1(); break;
case 0xA: ... 0x23: method2(); break;
}

I thought they were standard, but I remember reading somewhere this was
a GNU extension. I want to remove them from my code if they are
non-standard.

Non-standard. Replace them with:

if (somevar >= 0 && somevar <= 9) { method1(); }
else if (somevar >= 0xa && somevar <= 0x23) { method2(); }

-dr
 
M

Marcus Kwok

Dave Rahardja said:
Non-standard. Replace them with:

if (somevar >= 0 && somevar <= 9) { method1(); }
else if (somevar >= 0xa && somevar <= 0x23) { method2(); }

Or alternately, you can use the "fallthrough" behavior of the case
statements, depending on how much you wish to type:

switch (somevar) {
case 0:
case 1:
case 2:
/* you can fill in the rest here */
case 9:
method1();
break;

case 0xA:
/* ... */
case 0x23:
method2();
break;
}
 
I

Ivan Novick

Or alternately, you can use the "fallthrough" behavior of the case
statements, depending on how much you wish to type:

switch (somevar) {
case 0:
case 1:
case 2:
/* you can fill in the rest here */
case 9:
method1();
break;

case 0xA:
/* ... */
case 0x23:
method2();
break;
}
Umm, surely the generated machine code for this, must have to do more
comparisons if you have lots of cases than using ranges with greater
than and less than comparisons, hence this code should be slower.

Ivan
http://www.0x4849.net
 
P

peter koch

Umm, surely the generated machine code for this, must have to do more
comparisons if you have lots of cases than using ranges with greater
than and less than comparisons, hence this code should be slower.
No. When all cases are "dense", the compiler typically arranges the
code above to a jumptable - something like

if (somevar < 0 || somevar > 0x23) goto L_done;
jump jump_table[somevar]
 
P

peter koch

Umm, surely the generated machine code for this, must have to do more
comparisons if you have lots of cases than using ranges with greater
than and less than comparisons, hence this code should be slower.

Nope. Typically the code gets faster as the compiler will use a jump-
table: indexing a vector with somevar gives an instruction pointer
which gets jumped to. The only comparisons would be against minimum
and maximun value. For the example something like:

if (somevar < 0 || somevar > 0x23) goto L_done;
goto jump_table[somevar]; // obviously pseudo-code here

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top