Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of default initialization changed in C++11?

C++2003 8.5/5 says:

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.

[Emphasis added.]

The C++2011 standard changed that last item to

— otherwise, no initialization is performed.

This seems like it would be a breaking change for some programs. Was this intentional?

Edit

Here's some code to motivate this question:

class Foo {   public:     Foo() : m_values() {}      int m_values[3]; }; 

Before C++11, I thought the explicit mention of m_values in the default constructor would default-initialize that array. And since the elements of the array are scalar, I expected that to mean the values were all set to 0.

In C++11, it seems there's no longer a guarantee that this will happen. But maybe, as Mooing Duck pointed out in the comments, perhaps this is no longer a case of default initialization but some other form which preserves the expected behavior. Citations welcome.

like image 875
Adrian McCarthy Avatar asked Mar 06 '14 18:03

Adrian McCarthy


People also ask

What is default initialisation?

This is the initialization performed when an object is constructed with no initializer.

What is initializer in C?

In C/C99/C++, an initializer is an optional part of a declarator. It consists of the '=' character followed by an expression or a comma-separated list of expressions placed in curly brackets (braces).

What does default mean in C++?

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.

What does constructor default do?

In both Java and C#, a "default constructor" refers to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class. The default constructor implicitly calls the superclass's nullary constructor, then executes an empty body.

What are the effects of Default initialization in C++?

The effects of default initialization are: if T is a non-POD (until C++11) class type, the constructors are considered and subjected to overload resolution against the empty argument list. if T is an array type, every element of the array is default-initialized;

Which types are not initialized by default in C++?

Scalars and POD types with dynamic storage duration were considered to be not initialized (since C++11, this situation was reclassified as a form of default initialization). each direct non-static data member M of T is of class type X (or array thereof), X is const-default-constructible, and

How to initialize a container in C++11?

In C++11 you can initialize containers intuitively: Similarly, C++11 supports in-class initialization of data members: is called a defaulted function. The =default; part instructs the compiler to generate the default implementation for the function.

What is Default initialization of non-class variables?

Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized ) If T is a const-qualified type, it must be a class type with a user-provided default constructor.


1 Answers

The final effects are almost the same. In C++03, the use of default-initialize was restricted to non-POD class type, so the last point never applied. In C++11, the standard simplifies the wording by eliminating the condition with regards to where default-initialization was used, and changes the definition of default-initialization to cover all of the cases in a way to correspond what happened before.

like image 93
James Kanze Avatar answered Sep 17 '22 20:09

James Kanze