显式调用析构函数 [英] calling destructor explicitly

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

问题描述

我知道在大多数情况下,我们不应该明确地调用析构函数。但是,我看到了一个例子从C ++ 11标准N3485部分13.4.5模板参数:


一个显式析构函数调用一个对象有一个类型
是类模板专门化可以显式指定
模板参数。示例:

 模板< class T& struct A {
〜A();
};

void f(A< int> * p,A< int> * q){
对> A& // OK:destructor call
q-> A< int> ::〜A< int>(); // OK:destructor call
}


对我来说,我们可以在这种情况下显式调用析构函数,你能解释一下为什么吗?那些析构函数调用在这个例子中是什么意思?为什么它们是合理的?



另一个问题:



我们可以称为析构函数除了当我们正在实施布置删除吗?



谢谢。
$ b

编辑:我从 C ++ FAQ ,我们不应该在局部变量上显式地调用析构函数。

解决方案


在我看来,我们可以在这种情况下显式调用析构函数,我为什么?


你是说我们为什么?因为语言允许对任何对象的显式析构函数调用。正如你所说,它通常会给出未定义的行为,因为大多数对象将以其他方式被销毁,它是未定义的行为,以销毁两次(或更普遍的是在销毁后访问它)。但这只是意味着你不能这样做,而不是语言会阻止你这样做。



还是你的意思是我们想要什么?


这个析构函数调用在这个例子中意味着什么?


它们都意思相同,并且等效于 p>〜A();他们调用对象的析构函数。示例演示,如果你想,你可以在这里提供模板参数。我不知道为什么你想要。


除了放置删除,我们可以调用析构函数是什么? p>

我认为您可以随时调用一个简单的析构函数你喜欢;但没有意义。我认为摧毁用新版本创造的东西是唯一合法的原因。


I understand that in most cases, we should not call a destructor explicitly. However, I saw an example from C++11 Standard N3485 Section 13.4.5 Template arguments:

An explicit destructor call for an object that has a type that is a class template specialization may explicitly specify the template-arguments. Example:

template<class T> struct A {
    ~A();
}; 

void f(A<int>* p, A<int>* q) {
    p->A<int>::~A();      // OK: destructor call
    q->A<int>::~A<int>(); // OK: destructor call
}

It seems to me that we can call destructor explicitly in this case, could you explain to me why? What does those destructor call mean in this example? Why they are reasonable?

Another question:

What are the cases that we can call destructors explicitly besides when we are implementing placement delete?

Thank you.

EDIT: I found from C++ FAQ that we should not explicitly call a destructor on a local variable.

解决方案

It seems to me that we can call destructor explicitly in this case, could you explain to me why?

Do you mean why can we? Because the language allows explicit destructor calls on any object. As you say, it usually gives undefined behaviour since most objects will be destroyed in some other way, and it's undefined behaviour to destroy anything twice (or more generally to access it after destruction). But that just means that you mustn't do it, not that the language will prevent you from doing it.

Or do you mean why would we want to? Because that's how you destroy an object created by placement new.

What does those destructor call mean in this example?

They both mean the same thing, and are equivalent to p->~A(); they call the object's destructor. The example is demonstrating that you can provide template arguments here if you want to. I'm not sure why you'd want to.

What are the cases that we can call destructors explicitly besides placement delete?

I think that you're allowed to call a trivial destructor (one that doesn't do anything) whenever you like; but there's no point. I think destroying something created with placement new is the only legitimate reason to do it.

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

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