Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are C++ switch statements limited to constant expressions?

I wanted to use macro functions in switch statements before I realized the statements need to be constant. Example (does not compile):

#define BAND_FIELD1(B)   (10 * B + 1)
...
#define BAND_FIELD7(B)   (10 * B + 7)

int B = myField % 10;
switch (myField) {
    case BAND_FIELD1(B):
        variable1[B] = 123;
        break;
    case BAND_FIELD7(B):
        variable7[B] = 321;
        break;
    ...
}

I rather had to use if .. else:

if (myField == BAND_FIELD1(B)
    variable1[B] = 123;
else if (myField == BAND_FIELD7(B)
    variable7[B] = 321;

Why are the C++ switch statements limited to constant expressions?

like image 831
Jana Avatar asked Jan 24 '14 00:01

Jana


People also ask

What are the limitations of switch statement?

Switch case variables can have only int and char data type. So float or no data type is allowed. In this ch can be integer or char and cannot be float or any other data type.

Can label in switch statement must be constant only?

In C++ switch statement, the expression of each case label must be an integer constant expression.

Which expression is not allowed in switch C?

The value of the expressions in a switch-case statement must be an ordinal type i.e. integer, char, short, long, etc. Float and double are not allowed.

What is a constant expression in C?

A constant expression gets evaluated at compile time, not run time, and can be used in any place that a constant can be used. The constant expression must evaluate to a constant that is in the range of representable values for that type.


3 Answers

One of the strengths of C++ is its static checking. The switch statement is a static control flow construct, whose power lies in the ability to check (statically) whether all cases have been considered, and in being able to group cases sensibly (e.g. fall through common parts).

If you want to check conditions dynamically, you can already do so with a variety of techniques (if statements, conditional operators, associative arrays, virtual functions, to name a few).

like image 153
Kerrek SB Avatar answered Oct 21 '22 21:10

Kerrek SB


The compiler can generate the fastest possible code for a switch when presented with constants--e.g. jump tables or binary search trees.

When given non-constant values, it can't generate code that's any faster than chained if-else statements. Which you already have at your disposal anyway!

like image 39
StilesCrisis Avatar answered Oct 21 '22 22:10

StilesCrisis


Why are the c++ switch statements limited to constant expressions?

Because the check performed by the switch statements are static. This means that the expressions need to be known at compile time.

In C++11 you can use constexpr (if the expressions are derivated by other constant expressions) in your favor. For example consider this function (that replaces your #define):

inline constexpr int BAND_FIELD1(int B) {
    return 10 * B + 1;
}

used in the following simplified version of your code:

constexpr int myField = 0;
constexpr int B = myField % 10;

int variable1 = 0;
switch (myField) {
    case BAND_FIELD1(B):
        variable1 = 123;
        break;
    // ...
    default: break;
}

As you can see, the above code will easily compile.

like image 5
Shoe Avatar answered Oct 21 '22 22:10

Shoe