来自《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} }
的评估结束?
S* p = new S{1, {2,3}}
。 - Jarod42