Initializing variables (int32) inside switch statement
This question already has an answer here:
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.
上一篇: 在开关块内声明的变量