加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:43:05 所属栏目:百科 来源:网络整理
导读:当我正在阅读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 progra
当我正在阅读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,所以如果你跳过它就会有所不同.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读