Self-referential default member initializers
11.9.3 [class.base.init]
Richard Smith

Created on 2016-08-29.00:00:00 last changed 20 months ago


Date: 2018-11-15.00:00:00

Proposed resolution (November, 2018):

Change 9.4.2 [dcl.init.aggr] paragraph 12 as follows:

If a reference member is initialized from its has a default member initializer and a potentially-evaluated subexpression thereof is an aggregate initialization that would use that default member initializer, the program is ill-formed. [Example:
  struct A;
  extern A a;
  struct A {
    const A& a1 { A{a,a} }; // OK
    const A& a2 { A{} };    // error
  A a{a,a};                 // OK

  struct B {
  int n = B{}.n;            // error

end example]

Date: 2018-03-15.00:00:00

Notes from the March, 2018 meeting:

There was a suggestion that creating an object of the containing class in a default member initializer should be prohibited. That would presumably be a difference between the reference member and non-reference member cases, since the intent is to allow creation of a temporary for a reference member to bind to. The suggested approach for drafting was simply to remove the restriction to references in 9.4.2 [dcl.init.aggr] paragraph 11.

Date: 2019-02-15.00:00:00

[Accepted as a DR at the February, 2019 meeting.]

Consider an example like:

  struct A {
    int n = A{}.n;

There doesn't seem to be a good reason to support this kind of thing, and it would be simpler to say that a default member initializer can't trigger any direct or indirect use of itself in general, rather than just the two special cases that were banned by issues 1696 and 1397.

Date User Action Args
2020-12-15 00:00:00adminsetmessages: + msg6408
2020-12-15 00:00:00adminsetstatus: drafting -> cd5
2018-04-11 00:00:00adminsetmessages: + msg6198
2018-04-11 00:00:00adminsetstatus: open -> drafting
2016-08-29 00:00:00admincreate