为什么可以进入没有初始化程序的标量类型对象的范围呢? [英] Why is it OK to jump into the scope of an object of scalar type w/o an initializer?

查看:105
本文介绍了为什么可以进入没有初始化程序的标量类型对象的范围呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我阅读C ++标准时,根据该标准,似乎下面的代码是完美的.

When I'm reading the C++ standard, it seems that the following code is perfectly fine according to the standard.

int main() {
   goto lol;
   {
      int x;
lol:
      cout << x << endl;
   }
}

// OK

[n3290:6.7/3]:可以转移到一个块中,但不能转移到一个块中 通过初始化绕过声明的方法.一个程序 从具有自动存储持续时间的变量为 不在范围内直至其范围不正确,除非 变量具有标量类型,类类型具有默认值 构造函数和琐碎的析构函数,其中之一的cv限定版本 这些类型,或前面的类型之一的数组,并已声明 没有初始化器.

[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.

为什么应该起作用?跳过其定义并使用未定义的x还是危险吗?为什么初始化程序的存在会有所不同?

Why should it work? Isn't it still dangerous to jump over its definition and use undefined x? And why should the existence of an initializer make any difference?

推荐答案

无论如何,您都将使用未初始化的x,因为int x;与其将要得到的未初始化一样.初始化程序的存在当然会有所不同,因为您会跳过它.例如,int x = 5;会初始化x,因此无论是否跳过它,都会有所不同.

You'd use an uninitialized x anyways, since int x; is as uninitialized as it's going to get. The existence of an initializer makes of course a difference, because you'd be skipping it. int x = 5; for example initializes x, so it would make a difference if you jumped over it or not.

这篇关于为什么可以进入没有初始化程序的标量类型对象的范围呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆