The initialization of "unused" is skipped by "goto label" - why do I get it for std :: string, but not for int?

I ran into this error in some kind of code, and after several experiments I came across this oddity. - I get it for std::string, but notint

For std::stringI get error C2362: initialization of 'unused' is skipped by 'goto label':

{   goto label;
    std::string unused;
label:;
}

For int I do not get any error , however:

{   goto label;
    int unused = 10;
label:;
}

Why is the difference? Is it because it std::stringhas a nontrivial destructor?

+4
source share
2 answers

++ 6.7 , ( ):

, , . , 87 , , , , , , cv- (8.5).

:

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
}

, , , , :

goto label;
      int unused ;
label:

, Visual Studio , gcc clang , gcc :

error:   crosses initialization of 'int unused'
       int unused = 10;
           ^

, Visual Studio , , , , clang gcc .

, 467, ( ):

[...] , , ( "" ), , [...]

+4

. . , :

{
    goto label;
    int unused;
    unused = 10;
label:
    ;
}

std::string unused; int unused = 10; ( std::string), . , , , :

switch ( something )
{
    int i;
case 0:
    i = x;
    // ...
    break;

case 1:
    i = y;
    //  ...
    break;
//  ...
}

, C, ++ .

+3

Source: https://habr.com/ru/post/1535028/


All Articles