Initializing variables (int32) inside switch statement

This question already has an answer here:

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

  • Try adding {} :

    default:
    {
        int32 x = 1;
        doSomething(x);
        break;
    }
    

    According to the standard:

    It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps 91 from a point where a variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless ....

    void f() {
        // ...
        goto lx; // ill-formed: jump into scope of a
            // ...
        ly:
            X a = 1;
            // ...
        lx:
            goto ly; // OK, jump implies destructor
                     // call for a followed by construction
                     // again immediately following label ly
    }
    

    91) The transfer from the condition of a switch statement to a case label is considered a jump in this respect.

    链接地址: http://www.djcxy.com/p/14798.html

    上一篇: 在开关块内声明的变量

    下一篇: 初始化switch语句中的变量(int32)