Title
Value-initialization with multiple default constructors
Status
tentatively ready
Section
9.4.1 [dcl.init.general]
Submitter
Brian Bi

Created on 2024-02-09.00:00:00 last changed 3 weeks ago

Messages

Date: 2024-04-05.21:08:10

Proposed resolution (approved by CWG 2024-04-05):

  1. Change in 6.7.3 [basic.life] paragraph 1 as follows:

    The lifetime of an object or reference is a runtime property of the object or reference. A variable is said to have vacuous initialization if it is default-initialized, no other initialization is performed, and, if it is of class type or a (possibly multidimensional) array thereof, a trivial constructor of that class type has a trivial default constructor is selected for the default-initialization. The lifetime of an object of type T begins when: ...
  2. Change in 9.4.1 [dcl.init.general] paragraph 9 as follows:

    To value-initialize an object of type T means:
    • If T is a (possibly cv-qualified) class type (Clause 11 [class]), then
      • if T has either no default constructor (11.4.5.2 [class.default.ctor]) or a default constructor that is user-provided or deleted, then the object is default-initialized;
      • otherwise, the object is zero-initialized and then default-initialized.
      let C be the constructor selected to default-initialize the object, if any. If C is not user-provided, the object is first zero-initialized. In all cases, the object is then default-initialized.
    • ...
Date: 2024-04-05.21:08:10

(From submission #501.)

Subclause 9.4.1 [dcl.init.general] paragraph 9 specifies (as modified by issue 2820):

To value-initialize an object of type T means:
  • If T is a (possibly cv-qualified) class type (Clause 11 [class]), then
    • if T has either no default constructor (11.4.5.2 [class.default.ctor]) or a default constructor that is user-provided or deleted, then the object is default-initialized;
    • otherwise, the object is zero-initialized and then default-initialized.
  • ...

Per the specification, no zero-initialization occurs if the class has any user-provided default constructor, even if that constructor is not actually selected for the default initialization. This is observable in a constant expression, where reads of uninitialized data members are ill-formed.

History
Date User Action Args
2024-04-05 21:08:10adminsetstatus: open -> tentatively ready
2024-02-11 22:24:22adminsetmessages: + msg7604
2024-02-09 00:00:00admincreate