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

断了今生、忘了曾经 提交于 2019-12-08 15:15:14

问题


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.

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?

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

To default-initialize an object of type T means:

— 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 object is zero-initialized.

To value-initialize an object of type T means:

— 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);

— 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;

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

— 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. To be honest I'm having trouble parsing the standardese here and I don't understand the difference between the definitions.


回答1:


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

To default-initialize an object of type T means:

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

And in paragraph 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:

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:

To value-initialize an object of type T means:

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

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

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

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. 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! […]



来源:https://stackoverflow.com/questions/27349679/is-value-initialization-part-of-the-c98-standard-if-not-why-was-it-added-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!