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

后端 未结 23 3766
一生所求
一生所求 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:08

    I wrote this answer orginally for this question. However when I finished it I found that answer has been closed. So I posted it here, maybe someone who likes references to standard will find it helpful.

    Original Code in question:

    int i;
    i = 2;
    switch(i)
    {
        case 1: 
            int k;
            break;
        case 2:
            k = 1;
            cout<

    There are actually 2 questions:

    1. Why can I declare a variable after case label?

    It's because in C++ label has to be in form:

    N3337 6.1/1

    labeled-statement:

    ...

    • attribute-specifier-seqopt case constant-expression : statement

    ...

    And in C++ declaration statement is also considered as statement (as opposed to C):

    N3337 6/1:

    statement:

    ...

    declaration-statement

    ...

    2. Why can I jump over variable declaration and then use it?

    Because: N3337 6.7/3

    It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps (The transfer from the condition of a switch statement to a case label is considered a jump in this respect.)

    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 the variable has scalar type, class type with a trivial default constructor and a trivial destructor, a cv-qualified version of one of these types, or an array of one of the preceding types and is declared without an initializer (8.5).

    Since k is of scalar type, and is not initialized at point of declaration jumping over it's declaration is possible. This is semantically equivalent:

    goto label;
    
    int x;
    
    label:
    cout << x << endl;
    

    However that wouldn't be possible, if x was initialized at point of declaration:

     goto label;
    
        int x = 58; //error, jumping over declaration with initialization
    
        label:
        cout << x << endl;
    

提交回复
热议问题