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

标签 c++ reference new-operator object-lifetime temporary-objects

来自 [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/

相关文章:

空体的 C++ 函数与没有体的函数

c++ - "static property"和静态常量值有什么区别?

c++ - 我怎样才能有一个受约束的运算符模板?

c++ - C++ 中的引用是如何编码的?

c++ - clang:存在公共(public)虚拟继承时无法转换为私有(private)基础

variables - Joomla 为什么 JFactory 经常通过引用传递

c++将const对象引用传递给函数

c++ - 在 STL 实现中放置新的 vs 赋值

eclipse - 如何在 Eclipse 中显示 SVG?

安卓应用 : How to create a new window?