C ++ - 析构函数何时调用已使用但未分配的对象?

时间:2022-09-25 18:47:50

Example:

std::unique_ptr<int> GetPtr() { return std::unique_ptr<int>(new int(5)); }
...
void DoSomething() {
  int x = 2 + *GetPtr();
}

When is the destructor of the unique_ptr returned by GetPtr() called within DoSomething? Is it called after the call to 'operator+' or is it called once we leave the scope of 'DoSomething'?

何时在DoSomething中调用GetPtr()返回的unique_ptr的析构函数?它是在调用'operator +'后调用还是在我们离开'DoSomething'的范围后调用它?

Thanks.

2 个解决方案

#1


3  

The draft standard N3936 S12.2/3 says:

标准草案N3936 S12.2 / 3说:

Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created.

临时对象作为评估全表达式(1.9)的最后一步被销毁,该表达式(词法上)包含创建它们的点。

S1.9/10:

A full-expression is an expression that is not a subexpression of another expression.

完整表达式是不是另一个表达式的子表达式的表达式。

S1.9/14:

Every value computation and side effect associated with a full-expression is sequenced before every value computation and side effect associated with the next full-expression to be evaluated.

在每个值计算和与要评估的下一个全表达式相关联的副作用之前,对与全表达式相关联的每个值计算和副作用进行排序。

Note 8:

8) As specified in 12.2, after a full-expression is evaluated, a sequence of zero or more invocations of destructor functions for temporary objects takes place, usually in reverse order of the construction of each temporary object.

8)如12.2中所述,在评估完整表达式之后,临时对象的析构函数的零次或多次调用发生的顺序通常与每个临时对象的构造相反。

The full expression is the RHS of the assignment, and the sequence point occurs once that computation is complete. The temporary is destroyed after the '+' computation and before the assignment.

完整表达式是赋值的RHS,并且一旦计算完成就会出现序列点。在'+'计算之后和赋值之前,临时被销毁。

#2


3  

I believe it is called before operator+ is called, because GetPtr() is called, then the result of it is dereference, and copied. the reference to the unique pointer belongs to the scope of the parameters of the + operator. The last reference to the value is lost when the operator* is done, which is before the operator+ is even called.

我相信它在调用operator +之前被调用,因为调用了GetPtr(),然后它的结果是取消引用并被复制。对唯一指针的引用属于+运算符的参数范围。当操作符*完成时,即在调用operator +之前,对该值的最后引用将丢失。

#1


3  

The draft standard N3936 S12.2/3 says:

标准草案N3936 S12.2 / 3说:

Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created.

临时对象作为评估全表达式(1.9)的最后一步被销毁,该表达式(词法上)包含创建它们的点。

S1.9/10:

A full-expression is an expression that is not a subexpression of another expression.

完整表达式是不是另一个表达式的子表达式的表达式。

S1.9/14:

Every value computation and side effect associated with a full-expression is sequenced before every value computation and side effect associated with the next full-expression to be evaluated.

在每个值计算和与要评估的下一个全表达式相关联的副作用之前,对与全表达式相关联的每个值计算和副作用进行排序。

Note 8:

8) As specified in 12.2, after a full-expression is evaluated, a sequence of zero or more invocations of destructor functions for temporary objects takes place, usually in reverse order of the construction of each temporary object.

8)如12.2中所述,在评估完整表达式之后,临时对象的析构函数的零次或多次调用发生的顺序通常与每个临时对象的构造相反。

The full expression is the RHS of the assignment, and the sequence point occurs once that computation is complete. The temporary is destroyed after the '+' computation and before the assignment.

完整表达式是赋值的RHS,并且一旦计算完成就会出现序列点。在'+'计算之后和赋值之前,临时被销毁。

#2


3  

I believe it is called before operator+ is called, because GetPtr() is called, then the result of it is dereference, and copied. the reference to the unique pointer belongs to the scope of the parameters of the + operator. The last reference to the value is lost when the operator* is done, which is before the operator+ is even called.

我相信它在调用operator +之前被调用,因为调用了GetPtr(),然后它的结果是取消引用并被复制。对唯一指针的引用属于+运算符的参数范围。当操作符*完成时,即在调用operator +之前,对该值的最后引用将丢失。