在新初始化程序中绑定到引用的临时对象的生命周期是多久?

4

来自《C++编程语言标准》[class.temporary]一章:

(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]

这是否意味着绑定到S的引用成员mp的临时对象{2,3}会一直存在,直到评估表达式new S { 1, {2,3} }或者表达式S* p = new S{ 1, {2,3} }的评估结束?

6
完整表达式为 S* p = new S{1, {2,3}} - Jarod42
@Jarod42 谢谢,您想发表一个答案吗? - Géry Ogam
1个回答

1
完整表达式是 S* p = new S{1, {2,3}}

更加技术化地说,这不是一个表达式,但是完整的表达式在这里包括了 p 的初始化([intro.execution]/5)。并不是初始化部分对于对象生命周期很重要。 - aschepler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接