4

From [class.temporary] of the Working Draft, Standard for Programming Language C++:

(6.12) — A temporary bound to a reference in a new-initializer ([expr.new]) persists until the completion of the full-expression containing the new-initializer.

[Note 7: This might introduce a dangling reference. — end note]

[Example 5:

struct S { int mi; const std::pair<int,int>& mp; };
S a { 1, {2,3} };
S* p = new S{ 1, {2,3} };       // creates dangling reference

end example]

Does it mean that the temporary object {2,3} bound to the reference member mp of S persists until the evaluation of the expression new S { 1, {2,3} }, or until the evaluation of the expression S* p = new S{ 1, {2,3} }?

Géry Ogam
  • 6,336
  • 4
  • 38
  • 67

1 Answers1

1

Full-expression is S* p = new S{ 1, {2,3} }.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • To be a little more technical, that's not an expression, but the full-expression here does include the initialization of `p` ([intro.execution]/5). Not that the initialization part matters for object lifetime. – aschepler Sep 07 '21 at 17:57