Title
Value initialization and default constructors
Status
cd1
Section
9.4 [dcl.init]
Submitter
Mike Miller

Created on 2005-10-27.00:00:00 last changed 197 months ago

Messages

Date: 2008-09-15.00:00:00

[Voted into the WP at the September, 2008 meeting (resolution in paper N2762).]

Date: 2008-09-15.00:00:00

Notes from the September, 2008 meeting:

The resolution supplied in paper N2762 differs from the June, 2008 proposed resolution in that the implicitly-declared default constructor is only called (and thus defined) if it is non-trivial, making the struct POD example above well-formed.

Date: 2008-06-15.00:00:00

Proposed resolution (June, 2008):

Change the second bullet of the value-initialization definition in 9.4 [dcl.init] paragraph 5 as follows:

  • if T is a non-union class type without a user-provided constructor, then every non-static data member and base-class component of T is value-initialized; [Footnote: Value-initialization for such a class object may be implemented by zero-initializing the object and then calling the default constructor. —end footnote] the object is zero-initialized and the implicitly-defined default constructor is called;

Date: 2006-10-15.00:00:00

Notes from the October, 2006 meeting:

The CWG now prefers that it should not be left unspecified whether programs of this sort are well- or ill-formed; instead, the Standard should require that the default constructor be defined in such cases. Three possibilities of implementing this decision were discussed:

  1. Change 6.3 [basic.def.odr] to state flatly that the default constructor is used by value initialization (removing the implication that 9.4 [dcl.init] determines the conditions under which it is used).

  2. Change 9.4 [dcl.init] to specify that non-union class objects with no user-declared constructor are value-initialized by first zero-initializing the object and then calling the (implicitly-defined) default constructor, replacing the current specification of value-initializing each of its sub-objects.

  3. Add a normative statement to 9.4 [dcl.init] that value-initialization causes the implicitly-declared default constructor to be implicitly defined, even if it is not called.

Date: 2006-04-15.00:00:00

Notes from the April, 2006 meeting:

The approach favored by the CWG was to leave 6.3 [basic.def.odr] unchanged and to add normative wording to 9.4 [dcl.init] indicating that it is unspecified whether the default constructor is called.

Date: 2022-09-25.18:08:42

The wording resulting from the resolution of issue 302 does not quite implement the intent of the issue. The revised wording of 6.3 [basic.def.odr] paragraph 2 is:

A default constructor for a class is used by default initialization or value initialization as specified in 9.4 [dcl.init].

This sounds as if 9.4 [dcl.init] specifies how and under what circumstances value initialization uses a default constructor (which was, in fact, the case for default initialization in the original wording). However, the normative text there makes it plain that value initialization does not call the default constructor (the permission granted to implementations to call the default constructor for value initialization is in a non-normative footnote).

The example that occasioned this observation raises an additional question. Consider:

    struct POD {
      const int x;
    };

    POD data = POD();

According to the (revised) resolution of issue 302, this code is ill-formed because the implicitly-declared default constructor will be implicitly defined as a result of being used by value initialization (11.4.5 [class.ctor] paragraph 7), and the implicitly-defined constructor fails to initialize a const-qualified member (11.9.3 [class.base.init] paragraph 4). This seems unfortunate, because the (trivial) default constructor of a POD class is otherwise not used — default initialization applies only to non-PODs — and it is not actually needed in value initialization. Perhaps value initialization should be defined to “use” the default constructor only for non-POD classes? If so, both of these problems would be resolved by rewording the above-referenced sentence of 6.3 [basic.def.odr] paragraph 2 as:

A default constructor for a non-POD class is used by default initialization or value initialization as specified in (9.4 [dcl.init]).
History
Date User Action Args
2008-10-05 00:00:00adminsetmessages: + msg1826
2008-10-05 00:00:00adminsetmessages: + msg1825
2008-10-05 00:00:00adminsetstatus: review -> cd1
2008-06-29 00:00:00adminsetmessages: + msg1702
2008-06-29 00:00:00adminsetstatus: drafting -> review
2006-11-05 00:00:00adminsetmessages: + msg1415
2006-04-22 00:00:00adminsetmessages: + msg1333
2006-04-22 00:00:00adminsetstatus: open -> drafting
2005-10-27 00:00:00admincreate