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

2024-03-20


1697. Lifetime extension and copy elision

Section: 6.7.7  [class.temporary]     Status: CD4     Submitter: Richard Smith     Date: 2013-06-01

[Adopted as paper P0135R1 at the June, 2016 meeting.]

In an example like,

  struct S { ~S(); };
  struct X { X(); X(const X&); };
  struct T { S &&s; X x; };
  void f();
  void g() { T t = T{ {}, {} }; f(); }

it appears that the current wording allows two ways of handling this:

  1. The copy to t in g is not elided. X(const X&) is called, then ~S() is called, then f() is called.

  2. The copy to t in g is elided, so the temporary and t are the same object. Thus, the S object's lifetime is extended to the lifetime of the reference t.s, so first f() is called, then ~S() is called (and X(const X&) is not called).

However, EDG and g++ produce a third behavior: they do not call X(const X&), but they destroy the S() temporary at the end of its full-expression. The current wording does not appear to permit this behavior, but it seems preferable that lifetime extension does not depend on whether copy elision is done.