gpt4 book ai didi

C++ 返回对临时对象的引用或将它们存储在对象中

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:16:15 26 4
gpt4 key购买 nike

考虑以下处理 const 引用的代码:

const int & func (const int &x)
{
return x;
}

struct Foo {
Foo (const int &x)
: m_x(x) {}

const int & getX ()
{ return m_x; }

const int &m_x;
};

我想知道现在允许以下哪些(如果有的话):

int x = func(int(7));
int y = Foo(int(7)).getX();

是否可以保证临时 int 对象在被赋值或 getX 使用之前仍然存在?

更新:看来这是安全的 - 但究竟是为什么呢?

  1. 是否因为临时对象以递归方式绑定(bind)到 const 引用,并且只要对它们的绑定(bind)引用存在,就保证存在?
  2. 还是因为它们保证在完整表达式的持续时间内存在?

考虑存储指针而不是引用的边缘情况:

struct Foo {
Foo (const int &x)
: m_x(&x) {}

const int & getX ()
{ return *m_x; }

const int *m_x;
};

int y = Foo(int(7)).getX();

看来,如果情况 1) 是正确的,那么这将行不通。但如果情况 2) 是正确的,它会。

最佳答案

两者都是安全的,因为您将值复制到 xy 中。临时变量在完整表达式结束之前有效。

12.2 临时对象

4) There are two contexts in which temporaries are destroyed at a different point than the end of the full-expression. The first context is when a default constructor is called to initialize an element of an array. If the constructor has one or more default arguments, any temporaries created in the default argument expressions are destroyed immediately after return from the constructor.

5) The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except as specified below. A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits. A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call. A temporary bound to the returned value in a function return statement (6.6.3) persists until the function exits. In all these cases, the temporaries created during the evaluation of the expression initializing the reference, except the temporary to which the reference is bound, are destroyed at the end of the full-expression in which they are created and in the reverse order of the completion of their construction. If the lifetime of two or more temporaries to which references are bound ends at the same point, these temporaries are destroyed at that point in the reverse order of the completion of their construction. In addition, the destruction of temporaries bound to references shall take into account the ordering of destruction of objects with static or automatic storage duration (3.7.1, 3.7.2); that is, if obj1 is an object with static or automatic storage duration created before the temporary is created, the temporary shall be destroyed before obj1 is destroyed; if obj2 is an object with static or automatic storage duration created after the temporary is created, the temporary shall be destroyed after obj2 is destroyed. [ Example:

class C 
{
/ / ...
public :
C();
C(int );
friend C operator +(const C&, const C&);
~C();
};
C obj1 ;
const C& cr = C (16)+ C (23);
C obj2 ;

the expression C(16)+C(23) creates three temporaries. A first temporary T1 to hold the result of the expression C(16), a second temporary T2 to hold the result of the expression C(23), and a third temporary T3 to hold the result of the addition of these two expressions. The temporary T3 is then bound to the reference cr. It is unspecified whether T1 or T2 is created first. On an implementation where T1 is created before T2, it is guaranteed that T2 is destroyed before T1. The temporaries T1 and T2 are bound to the reference parameters of operator+; these temporaries are destroyed at the end of the full expression containing the call to operator+. The temporary T3 bound to the reference cr is destroyed at the end of cr’s lifetime, that is, at the end of the program. In addition, the order in which T3 is destroyed takes into account the destruction order of other objects with static storage duration. That is, because obj1 is constructed before T3, and T3 is constructed before obj2, it is guaranteed that obj2 is destroyed before T3, and that T3 is destroyed before obj1. —end example ]

关于C++ 返回对临时对象的引用或将它们存储在对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12551884/

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