值初始化是C ++ 98标准的一部分吗?如果不是,为什么将它添加到C ++ 03标准中? [英] Is value initialization part of the C++98 standard? If not, why was it added in the C++03 standard?

查看:118
本文介绍了值初始化是C ++ 98标准的一部分吗?如果不是,为什么将它添加到C ++ 03标准中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

干杯和健康. -Alf在答案中发表了评论,与C ++相比,值初始化可以说是C ++ 03的新功能. 98.我想知道他是什么意思.

Cheers and hth. - Alf made a comment in this answer that value initialization is arguably a new feature of C++03 compared to C++98. I wonder what he meant.

C ++ 98的值初始化的一部分吗?它在概念上存在但在名称上不存在吗?为什么将它添加到C ++ 03标准?

Is value initialization part of C++98? Is it present in concept but not in name? Why was it added to the C++03 standard?

我有'03标准的副本,但没有'98标准的副本.这是默认初始化和值初始化的定义.

I have a copy of the '03 standard but not the '98 standard. Here's the definition of default initialization and value initialization.

默认初始化T类型的对象的意思是:

To default-initialize an object of type T means:

-如果T是非POD类类型(第9节),则默认构造函数为 T被调用(如果T没有,则初始化格式不正确 可访问的默认构造函数);

— if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

-如果T是数组类型,则每个元素都将默认初始化;

— if T is an array type, each element is default-initialized;

-否则,该对象将被初始化为零.

— otherwise, the object is zero-initialized.

要对T类型的对象进行值初始化,则意味着:

To value-initialize an object of type T means:

-如果T是类类型 (第9条)和用户声明的构造函数(12.1),然后使用默认 T的构造函数被调用(如果T初始化错误, 没有可访问的默认构造函数);

— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

-如果T是非工会类别 类型,而无需用户声明的构造函数,则每个非静态数据 T的成员和基类组件是值初始化的;

— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;

-如果T为 数组类型,然后每个元素都进行值初始化;

— if T is an array type, then each element is value-initialized;

-否则, 对象是零初始化的

— otherwise, the object is zero-initialized

我的猜测是'98具有默认初始化,但没有值初始化,并且两者之间存在一些关键区别.老实说,我在解析标准语时遇到了麻烦,而且我不理解定义之间的区别.

My guess is that '98 had default initialization but not value initialization and that there's some key difference between the two. To be honest I'm having trouble parsing the standardese here and I don't understand the difference between the definitions.

推荐答案

引用 ISO/IEC 14882:1998标准文件(从ISO撤回):

Quoting the ISO/IEC 14882:1998 standard document (that was withdrawn from ISO):

默认初始化类型为T的对象的意思是:

To default-initialize an object of type T means:

  • 如果T是非POD类类型(第9节),则调用T的默认构造函数(并且如果T没有,则初始化格式不正确. 可访问的默认构造函数);
  • 如果T是数组类型,则每个元素都将默认初始化;
  • 否则,该对象的存储将初始化为零.
  • if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is an array type, each element is default-initialized;
  • otherwise, the storage for the object is zero-initialized.

在第7段中:

其初始化程序是一组空括号(即())的对象应进行默认初始化.

An object whose initializer is an empty set of parentheses, i.e., (), shall be default-initialized.

有关更改依据的详细信息,请参见

Details on the rationale behind the change can be found in the defect report that made it happen:

此定义适用于局部变量,但不适用于 由于执行以下表达式而初始化的对象 形式为T(),因为此类表达式产生的对象将 立即复制,因此应具有以下值: 确保可以复制.
为此,我建议添加 以下是8.5第5段的新文本:

This definition is appropriate for local variables, but not for objects that are initialized as a result of executing expressions of the form T(), because the objects yielded by such expressions will be copied immediately, and should therefore have values that are assured of being copyable.
To this end, I propose adding the following new text to 8.5, paragraph 5:

值初始化类型为T的对象的意思是:

To value-initialize an object of type T means:

  • 如果T是具有用户声明的构造函数(12.1)的类类型(第9节[class]),则将调用T的默认构造函数(并且 如果T没有可访问的默认值,则初始化格式不正确 构造函数);
  • 如果T是没有用户声明的构造函数的类类型,则T的每个非静态数据成员和基类组件为 值初始化的;
  • 如果T是数组类型,则每个元素都进行值初始化;
  • 否则,对象的存储将初始化为零.
  • if T is a class type (clause 9 [class]) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is a class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
  • if T is an array type, then each element is value-initialized;
  • otherwise, the storage for the object is zero-initialized.

此外,我建议将默认初始化"更改为 5.2.3第2段中的值初始化".

In addition, I propose to change ‘‘default-initialization’’ to ‘‘value-initialization’’ in 5.2.3 paragraph 2.

然后,进行历史解释:

古代历史

从前,一位名叫Laura Eaves的AT& T编译器开发人员问道 我:‘‘int()的值应该是什么?’’我的第一个念头是 它应该与x说完之后的值相同

Once upon a time, an AT&T compiler developer named Laura Eaves asked me: ‘‘What should be the value of int()?’’ My first thought was that it should be the same value as x has after saying

int x;

但是我很快意识到那个定义是行不通的.原因是 x的值不确定(假设它是一个局部值 变量),但我们不介意x是不确定的,因为我们 大概在使用它之前先给x赋一个值.相比之下, int()最好没有不确定的值,因为复制 这样的值具有不确定的作用.禁止 编译器在编译过程中标记int()只是为了允许它 在执行过程中标记它! […]

but I soon realized that that definition would not do. The reason is that x has an indeterminate value (assuming that it is a local variable), but we don’t mind that x is indeterminate, because we are presumably going to assign a value to x before we use it. In contrast, int() had better not have an indeterminate value, because copying such a value has an undefined effect. It would be silly to forbid a compiler from flagging int() during compilation, only to allow it to flag it during execution! […]

这篇关于值初始化是C ++ 98标准的一部分吗?如果不是,为什么将它添加到C ++ 03标准中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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