Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 966
Praxeolitic Avatar asked Dec 08 '14 00:12

Praxeolitic


People also ask

Why is initializing a class member variable during declaration not permitted?

The main reason is that initialization applies to an object, or an instance, and in the declaration in the class there is no object or instance; you don't have that until you start constructing.

What is initialization in C?

Initialization is the process of locating and using the defined values for variable data that is used by a computer program. For example, an operating system or application program is installed with default or user-specified values that determine certain aspects of how the system or program is to function.

What is an initialization value?

In computer programming, initialization (or initialisation) is the assignment of an initial value for a data object or variable. The manner in which initialization is performed depends on the programming language, as well as the type, storage class, etc., of an object to be initialized.

Why do we need to initialize variables in C++?

In addition, when variables are initialized upon declaration, more efficient code is obtained than if values are assigned when the variable is used. A variable must always be initialized before use. Normally, the compiler gives a warning if a variable is undefined. It is then sufficient to take care of such cases.


1 Answers

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

like image 83
Columbo Avatar answered Oct 21 '22 15:10

Columbo