C++

C++ - Control Flow

switch-case

Posted by Rico's Nerd Cluster on January 15, 2023

switch-case

Like goto, switch-case is a label. Without {}, any variable declared within a switch-case statement has the scope of the entire statement. This will only work with variables that can be default-initialized, because they can be declared only with an intermediate value. This creates a problem: what if a variable that cannot be default initialized is created?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// A can be default-initialized
class A {
public:
  A ()=default;
};
class C{
public:
    C(){}   // This is NOT default initialization
};
int main()
{
    int i = 1;
    switch (i)
    {
    case 0:
        A j;    // Compiler implicitly calls default constructor, fine
        int b;  // POD can be declared only, fine.
        int d = 1;  // ERROR: jumping over initialization
        C c;    // ERROR:  jumping over initialization of 'C'
        break;
    case 1:
        b = 2;  // This is assignment, not initialization, fine
        break;
    }
}

This is a typical “Jump Over Initialization” error, where initialization may not happen before a variable gets used. (int d = 1; is another example, because its initialization may not be executed.)

In this case, the best practice is to limit the scope of the variable:

1
2
3
4
5
case 0:{
    int d = 1;  // fine
    C c;    // fine
    break;
}

Reference