伪析构函数调用不会销毁对象 [英] Pseudo-destructor call does not destroy an object

查看:203
本文介绍了伪析构函数调用不会销毁对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

#include <iostream>

typedef int t;
t a=42;

int main()
{
    a.t::~t();
    std::cout << a; //42
}



我希望 / code>将被销毁。但它不是真的,为什么?

I'm expected that a will be destroyed. But it is not true, why? How does do that pseudo-destructor call will be destroyed the object?

推荐答案


但是它不是true,为什么?

But it is not true, why?

§5.2.4/ 1:


唯一的效果是对点或箭头前的后缀表达式求值。

The only effect is the evaluation of the postfix-expression before the dot or arrow.

其中postfix-expression是调用发生的对象的表达式。因此,作为对平凡析构函数的调用的伪析构函数调用不会结束其应用于的对象的生命周期。例如,

Where the postfix-expression is the expression of the object for which the call takes place. Thus a pseudo destructor call, as a call to a trivial destructor, does not end the lifetime of the object it is applied to. For instance,

int i = 0;
(i += 5).~decltype(i)();
std::cout << i;

你实际上不能为标量调用析构函数,因为它们没有一个12.4)。该语句只允许在模板代码中调用其类型不知道的对象的析构函数 - 它不需要为标量类型编写专门化。

You can't actually call a destructor for scalars, because they don't have one (see §12.4). The statement is solely allowed for template code in which you call the destructor of an object whose type you don't know - it removes the necessity of writing a specialization for scalar types.

在注释中注意到,§5.2.4意味着

It was noted in the comments that §5.2.4 does imply the existence of a destructor for scalars by


或箭头 - >后使用伪析构函数名 operator
表示由 type-name 命名的非类型的析构函数。

The use of a pseudo-destructor-name after a dot . or arrow -> operator represents the destructor for the non-class type named by type-name.

但是,这与标准的其他部分不一致,例如§12,它调用析构函数一个特殊成员函数,并提到

However, this is inconsistent with other parts of the standard, e.g. §12, which calls a destructor a special member function and mentions that


析构函数用于销毁其类类型的对象。 p>

A destructor is used to destroy objects of its class type.

这似乎是在C ++ 98天中创建的不精确。

It appears to be an imprecision created in C++98 days.

这篇关于伪析构函数调用不会销毁对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆