绑定到新初始化程序中的引用的临时对象的生命周期是多少?
What is the lifetime of a temporary object bound to a reference in a new-initializer?
来自 工作草案的 [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]
是否表示绑定到S
的引用成员mp
的临时对象{2,3}
一直持续到表达式new S { 1, {2,3} }
的求值,或者直到求值表达式 S* p = new S{ 1, {2,3} }
?
完整表达式为 S* p = new S{ 1, {2,3} }
。
来自 工作草案的 [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]
是否表示绑定到S
的引用成员mp
的临时对象{2,3}
一直持续到表达式new S { 1, {2,3} }
的求值,或者直到求值表达式 S* p = new S{ 1, {2,3} }
?
完整表达式为 S* p = new S{ 1, {2,3} }
。