Title
Vacuous initialization for subobjects
Status
open
Section
6.7.3 [basic.life]
Submitter
Richard Smith

Created on 2024-10-31.00:00:00 last changed 3 weeks ago

Messages

Date: 2024-10-31.00:00:00

Consider:

  struct A {
    int x, y;
    constexpr A() : x((y = 1, y)) {}
  };
  constexpr int k = A().y;

"Vacuous initialization" is defined in 6.7.3 [basic.life] paragraph 1 only for variables, not for subobjects, so it is unclear whether the example is well-formed or not.

Possible resolution:

  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 is selected for the default-initialization. The lifetime of an object of type T begins when:
    • storage with the proper alignment and size for type T is obtained, and
    • its initialization (if any) is complete (including vacuous initialization) (9.4 [dcl.init]),
    except that if the object is a union member or subobject thereof, its lifetime only begins if that union member is the initialized member in the union (9.4.2 [dcl.init.aggr], 11.9.3 [class.base.init]), or as described in 11.5 [class.union], 11.4.5.3 [class.copy.ctor], and 11.4.6 [class.copy.assign], and except as described in 20.2.10.2 [allocator.members]. ...
  2. Change in 6.7.4 [basic.indet] paragraph 1 as follows:

    ... If no initialization is performed for an object (including subobjects a subobject) is not accessed during its initialization, such a byte retains its initial value until that value is replaced (9.4.1 [dcl.init.general], 7.6.19 [expr.ass]). If any bit in the value representation has an indeterminate value, the object has an indeterminate value; otherwise, if any bit in the value representation has an erroneous value, the object has an erroneous value (7.3.2 [conv.lval]).
  3. Change in 8.8 [stmt.dcl] paragraph 2 as follows:

    A variable is said to have vacuous initialization if it is default-initialized (9.4.1 [dcl.init.general]), 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 (11.4.5.2 [class.default.ctor]) is selected for the default-initialization. A block variable with automatic storage duration (6.7.5.4 [basic.stc.auto]) is active everywhere in the scope to which it belongs after its init-declarator . Upon each transfer of control (including sequential execution of statements) within a function from point P to point Q, all block variables with automatic storage duration that are active at P and not at Q are destroyed in the reverse order of their construction. Then, all block variables with automatic storage duration that are active at Q but not at P are initialized in declaration order; unless all such variables have vacuous initialization (6.7.3 [basic.life]), the transfer of control shall not be a jump. [ Footnote: ... ] When a declaration-statement is executed, P and Q are the points immediately before and after it; when a function returns, Q is after its body.
  4. Change in 9.4.1 [dcl.init.general] bullet 7.3 as follows:

    To default-initialize an object of type T means:
    • If T is a (possibly cv-qualified) class type (Clause Clause 11 [class]), constructors are considered. The applicable constructors are enumerated (12.2.2.4 [over.match.ctor]), and the best one for the initializer () is chosen through overload resolution (12.2 [over.match]). The constructor thus selected is called, with an empty argument list, to initialize the object.
    • If T is an array type, the semantic constraints of default-initializing a hypothetical element shall be met and each element is default-initialized.
    • Otherwise, no default initialization is performed completes without accessing the object.
History
Date User Action Args
2024-10-31 00:00:00admincreate