This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 118b. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-09-28


3063. Lifetime extension of temporaries past function return

Section: 6.8.7  [class.temporary]     Status: open     Submitter: Hubert Tong     Date: 2025-09-12

(From submission #760.)

Consider:

  struct B { ~B(); };
  struct A { const B &b; };
  A foo() { return {{}}; }   // #1
  void bar();
  int main() {
    A a = foo();
    bar();
  }

At #1, a temporary of type B is created and, due to guaranteed copy elision, is bound to a.b in main. The current rules, as amended by P2748R5 (Disallow Binding a Returned Glvalue to a Temporary), prescribe lifetime extension of that temporary, which is a novel requirement accidentally imposed by P2748R5 that is hard to implement.

Suggested resolution (Option 1):

Add in 6.8.7 [class.temporary] bullet 6.11 as follows:

Suggested resolution (Option 2):

  1. Move to before 6.8.2 [intro.object] paragraph 11 from 7.7 [expr.const] paragraph 2:

    The constituent values of an object o are
    • if o has scalar type, the value of o;
    • otherwise, the constituent values of any direct subobjects of o other than inactive union members.
    The constituent references of an object o are
    • any direct members of o that have reference type, and
    • the constituent references of any direct subobjects of o other than inactive union members.
    Some operations are described as implicitly creating objects ...
  2. Remove 7.7 [expr.const] paragraph 2 and change paragraph 3 as follows:

    The constituent values of an object o are
    • if o has scalar type, the value of o;
    • otherwise, the constituent values of any direct subobjects of o other than inactive union members.
    The constituent references of an object o are
    • any direct members of o that have reference type, and
    • the constituent references of any direct subobjects of o other than inactive union members.
    The constituent values and constituent references of a variable x are defined as follows:
    • If x declares an object, the constituent values and references of that object (6.8.2 [intro.object]) are constituent values and references of x.
    • If x declares a reference, that reference is a constituent reference of x.
    ...
  3. Add in 8.8.4 [stmt.return] paragraph 6 as follows:

    In a function whose return type is a reference, other than an invented function for std::is_convertible (21.3.8 [meta.rel]), a return statement that binds
    • the a returned reference or
    • a constituent reference (6.8.2 [intro.object]) of a returned object
    to a temporary expression (6.8.7 [class.temporary]) is ill-formed. [ Example: ... ]