Title
Temporary lifetime and non-static data member initializers
Status
cd4
Section
6.7.7 [class.temporary]
Submitter
Richard Smith

Created on 2013-05-31.00:00:00 last changed 86 months ago

Messages

Date: 2014-11-15.00:00:00

[Moved to DR at the November, 2014 meeting.]

Date: 2014-06-15.00:00:00

Proposed resolution (June, 2014):

  1. Add the following after 9.4.2 [dcl.init.aggr] paragraph 7:

  2. If a reference member is initialized from its brace-or-equal-initializer and a potentially-evaluated subexpression thereof is an aggregate initialization that would use that brace-or-equal-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
    

    If an aggregate class C contains a subaggregate...

  3. Delete the first bullet of 6.7.7 [class.temporary] paragraph 5:

  4. The second context is when a reference is bound to a temporary.117 The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except:

    • A temporary bound to a reference member in a constructor's ctor-initializer (11.9.3 [class.base.init]) persists until the constructor exits.

    • ...

  5. Insert the following as a new paragraph after 11.9.3 [class.base.init] paragraph 7:

  6. A temporary expression bound to a reference member in a mem-initializer is ill-formed. [Example:

      struct A {
        A() : v(42) { }  // error
        const int& v;
      };
    

    end example]

    In a non-delegating constructor, if a given potentially constructed subobject...

  7. Insert the following as a new paragraph after 11.9.3 [class.base.init] paragraph 9:

  8. A temporary expression bound to a reference member from a brace-or-equal-initializer is ill-formed. [Example:

      struct A {
        A() = default;          // OK
        A(int v) : v(v) { }     // OK
        const int& v = 42;      // OK
      };
      A a1;                     // error: ill-formed binding of temporary to reference
      A a2(1);                  // OK, unfortunately
    

    end example]

    In a non-delegating constructor, the destructor for each potentially constructed subobject...

This resolution also resolves issue 1815.

Date: 2014-03-03.00:00:00

Presumably a temporary bound to a reference in a non-static data member initializer should be treated analogously with what happens in a ctor-initializer, but the current wording of 6.7.7 [class.temporary] paragraph 5 is not clear on this point.

See also issue 1815 for similar questions regarding aggregate initialization.

History
Date User Action Args
2017-02-06 00:00:00adminsetstatus: drwp -> cd4
2015-05-25 00:00:00adminsetstatus: dr -> drwp
2015-04-13 00:00:00adminsetmessages: + msg5395
2014-11-24 00:00:00adminsetstatus: tentatively ready -> dr
2014-07-07 00:00:00adminsetmessages: + msg5081
2014-07-07 00:00:00adminsetstatus: drafting -> tentatively ready
2014-03-03 00:00:00adminsetstatus: open -> drafting
2013-05-31 00:00:00admincreate