cplusplus / CWG

Core Working Group
23 stars 7 forks source link

[class.cdtor] Referring to subobjects during aggregate initialization #555

Open MitalAshok opened 2 weeks ago

MitalAshok commented 2 weeks ago

Full name of submitter: Mital Ashok

Reference: [class.cdtor]

Issue description:

It is unclear how the first paragraph interacts with aggregate initialization for aggregates with non-trivial constructors (which can happen when a member or base has a non-trivial default/copy/move constructor). Taken literally, any class like this that is aggregate initialized can have it so the constructor never "begins execution", so you can never refer to any subobject. It seems like the intent is that the object has to start being constructed, even if a constructor isn't called, before you can refer to members.

struct B in Example 1 is one such struct that could be aggregate initialized.

Suggested resolution:

For an objecta class X with a non-trivial constructor, referring to any non-static member or base class of thean object of type cv X before the constructor of the object begins execution or before the first element ([dcl.init.aggr]p2) of X begins being initialized if X is an aggregate and the object was aggregate initialized ([dcl.init.list]) results in undefined behavior.

frederick-vs-ja commented 2 weeks ago

CWG1517 and #107 are related.

Again, I think such UB should be restricted to virtual base class subobjects and their subobjects.