简体   繁体   English

值初始化是C ++ 98标准的一部分吗? 如果没有,为什么它被添加到C ++ 03标准中?

[英]Is value initialization part of the C++98 standard? If not, why was it added in the C++03 standard?

Cheers and hth. 干杯和hth。 - Alf made a comment in this answer that value initialization is arguably a new feature of C++03 compared to C++98. - Alf在这个答案中发表评论说,与C ++ 98相比,值初始化可以说是C ++ 03的一个新特性。 I wonder what he meant. 我想知道他的意思。

Is value initialization part of C++98? 值初始化是C ++ 98的一部分吗? Is it present in concept but not in name? 它是出现在概念中而不是名义上吗? Why was it added to the C++03 standard? 为什么它被添加到C ++ 03标准中?

I have a copy of the '03 standard but not the '98 standard. 我有'03标准的副本,但不是'98标准。 Here's the definition of default initialization and value initialization. 这是默认初始化和值初始化的定义。

To default-initialize an object of type T means: 默认初始化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是非POD类类型(第9节),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是错误的);

— if T is an array type, each element is default-initialized; - 如果T是数组类型,则每个元素都是默认初始化的;

— otherwise, the object is zero-initialized. - 否则,对象被零初始化。

To value-initialize an object of type T means: 对值类型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是具有用户声明的构造函数(12.1)的类类型(第9节),则调用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是没有用户声明的构造函数的非联合类类型,则T的每个非静态数据成员和基类组件都是值初始化的;

— if T is an array type, then each element is value-initialized; - 如果T是数组类型,则每个元素都是值初始化的;

— otherwise, the object is zero-initialized - 否则,对象被零初始化

My guess is that '98 had default initialization but not value initialization and that there's some key difference between the two. 我的猜测是'98有默认初始化但不是值初始化,并且两者之间存在一些关键差异。 To be honest I'm having trouble parsing the standardese here and I don't understand the difference between the definitions. 说实话,我在解析这里的标准时遇到了麻烦,我不明白这些定义之间的区别。

Quoting the ISO/IEC 14882:1998 standard document (that was withdrawn from ISO): 引用ISO / IEC 14882:1998标准文件 (已从ISO中撤回):

To default-initialize an object of type T means: 默认初始化 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是非POD类类型(第9节),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是错误的);
  • if T is an array type, each element is default-initialized; 如果T是数组类型,则每个元素都是默认初始化的;
  • otherwise, the storage for the object is zero-initialized. 否则,对象的存储被零初始化。

And in paragraph 7: 在第7段中:

An object whose initializer is an empty set of parentheses, ie, () , shall be default-initialized. 初始化器为空的括号集(即()应默认初始化。

Details on the rationale behind the change can be found in the defect report that made it happen: 有关变更背后的基本原理的详细信息可以在发生变化的缺陷报告中找到:

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. 此定义适用于局部变量,但不适用于因执行T()形式的表达式而初始化的对象,因为这些表达式产生的对象将立即复制,因此应具有可确保的值。可复制。
To this end, I propose adding the following new text to 8.5, paragraph 5: 为此,我建议在8.5,第5段中增加以下新案文:

To value-initialize an object of type T means: 类型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); 如果T是具有用户声明的构造函数(12.1)的类类型(子句9 [class]),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是错误的);
  • 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; 如果T是没有用户声明的构造函数的类类型,那么T的每个非静态数据成员和基类组件都是值初始化的;
  • if T is an array type, then each element is value-initialized; 如果T是数组类型,那么每个元素都是值初始化的;
  • otherwise, the storage for the object is zero-initialized. 否则,对象的存储被零初始化。

In addition, I propose to change ''default-initialization'' to ''value-initialization'' in 5.2.3 paragraph 2. 另外,我建议在5.2.3第2段中将''default-initialization''改为''value-initialization''。

And, following that, a historical explanation: 然后,一个历史的解释:

Ancient history 古代历史

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 曾几何时,一位名叫Laura Eaves的AT&T编译器开发人员问我:'' int()的值应该是什么?''我的第一个想法是它应该与x之后的值相同

 int x; 

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. 原因是x有一个不确定的值(假设它是一个局部变量),但我们并不介意x是不确定的,因为我们可能会在使用它之前为x赋值。 In contrast, int() had better not have an indeterminate value, because copying such a value has an undefined effect. 相反, int()最好没有不确定的值,因为复制这样的值具有未定义的效果。 It would be silly to forbid a compiler from flagging int() during compilation, only to allow it to flag it during execution! 禁止编译器在编译期间标记int() ,只允许它在执行期间标记它是愚蠢的! […] [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM