Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling destructor explicitly

Tags:

c++

destructor

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.

like image 942
taocp Avatar asked May 23 '13 17:05

taocp


People also ask

How do you explicitly call a destructor of an object?

Use the obj. ~ClassName() Notation to Explicitly Call a Destructor Function. Destructors are special functions that get executed when an object goes out of scope automatically or is deleted by an explicit call by the user.

Should I explicitly call a destructor on a local variable?

[11.5] Should I explicitly call a destructor on a local variable? No! The destructor will get called again at the close } of the block in which the local was created. This is a guarantee of the language; it happens automagically; there's no way to stop it from happening.

Does delete call destructor C++?

When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor). If the operand to the delete operator is a modifiable l-value, its value is undefined after the object is deleted.


1 Answers

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.

like image 174
Mike Seymour Avatar answered Sep 17 '22 22:09

Mike Seymour