来自 [class.temporary]工作草案,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]
是否意味着临时对象
{2,3}
绑定(bind)到引用成员 mp
的 S
一直持续到表达式 new S { 1, {2,3} }
的计算, 或直到表达式 S* p = new S{ 1, {2,3} }
的计算?
最佳答案
全表达是S* p = new S{ 1, {2,3} }
.
关于c++ - 绑定(bind)到新初始化器中的引用的临时对象的生命周期是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69073827/