c – 为什么没有初始化器跳进标量类型对象的范围?

当我正在阅读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,所以如果你跳过它就会有所不同.

相关文章

对象的传值与返回说起函数,就不免要谈谈函数的参数和返回值...
从实现装饰者模式中思考C++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...