gpt4 book ai didi

c++ - 绑定(bind)到新初始化器中的引用的临时对象的生命周期是多少?

转载 作者:行者123 更新时间:2023-12-04 13:24:53 33 4
gpt4 key购买 nike

来自 [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)到引用成员 mpS一直持续到表达式 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com