Title
Lifetime extension and copy elision
Status
cd4
Section
6.7.7 [class.temporary]
Submitter
Richard Smith

Created on 2013-06-01.00:00:00 last changed 40 months ago

Messages

Date: 2016-06-15.00:00:00

[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.

History
Date User Action Args
2020-12-15 00:00:00adminsetstatus: drafting -> cd4
2013-10-14 00:00:00adminsetstatus: open -> drafting
2013-06-01 00:00:00admincreate