当我正在阅读C标准时,根据标准,以下代码似乎完全没问题.
int main() { goto lol; { int x; lol: cout << x << endl; } } // OK
[n3290: 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 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.
它为什么要起作用?跳过它的定义并使用undefined x不是很危险吗?为什么初始化器的存在会有什么不同?
解决方法
无论如何你都会使用未初始化的x,因为int x;就像它会得到的那样没有初始化.初始化程序的存在当然是有区别的,因为你要跳过它. int x = 5;例如,初始化x,所以如果你跳过它就会有所不同.