Why can't variables be declared in a switch statement?

后端 未结 23 3035
一生所求
一生所求 2020-11-21 05:15

I\'ve always wondered this - why can\'t you declare variables after a case label in a switch statement? In C++ you can declare variables pretty much anywhere (and declaring

23条回答
  •  南笙
    南笙 (楼主)
    2020-11-21 06:04

    Ok. Just to clarify this strictly has nothing to do with the declaration. It relates only to "jumping over the initialization" (ISO C++ '03 6.7/3)

    A lot of the posts here have mentioned that jumping over the declaration may result in the variable "not being declared". This is not true. An POD object can be declared without an initializer but it will have an indeterminate value. For example:

    switch (i)
    {
       case 0:
         int j; // 'j' has indeterminate value
         j = 0; // 'j' set (not initialized) to 0, but this statement
                // is jumped when 'i == 1'
         break;
       case 1:
         ++j;   // 'j' is in scope here - but it has an indeterminate value
         break;
    }
    

    Where the object is a non-POD or aggregate the compiler implicitly adds an initializer, and so it is not possible to jump over such a declaration:

    class A {
    public:
      A ();
    };
    
    switch (i)  // Error - jumping over initialization of 'A'
    {
       case 0:
         A j;   // Compiler implicitly calls default constructor
         break;
       case 1:
         break;
    }
    

    This limitation is not limited to the switch statement. It is also an error to use 'goto' to jump over an initialization:

    goto LABEL;    // Error jumping over initialization
    int j = 0; 
    LABEL:
      ;
    

    A bit of trivia is that this is a difference between C++ and C. In C, it is not an error to jump over the initialization.

    As others have mentioned, the solution is to add a nested block so that the lifetime of the variable is limited to the individual case label.

提交回复
热议问题