48
votes

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.

2
The referenced post is about calling a destructor explicitly on a local variable, whose destructor is also called again implicitly at the end of its scope.Andy Thomas
+1 i have done some similar nonsense in my code, calling the destructor explicitly to destroy my object(or atlest it seems to work). but i've done it on managed types. will be interesting to see the answersKoushik Shetty
@AndyThomas-Cramer I understand, what I meant is the explanation from answers that quotes the standard about explicit destructor calls. Is my post misleading? I will try to reword it if this is a problem.taocp
@taocp - Just clarifying here, as in the referenced post, that the undefined behavior is not calling a destructor explicitly - it's causing it to be called on the same object more than once.Andy Thomas
@AndyThomas-Cramer ah, I agree. I looked at the answers mainly. I will try to correct it. Thank you!taocp

2 Answers

33
votes

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.

29
votes

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

Because it's allowed by the language to be able to invoke the destructor of any object whenever you want (assuming you have access, e.g. it's not a private destructor).

What does those destructor call mean in this example?

It just invokes the destructor. Logically, it means that the object is destructed and should be considered to be garbage from that point on and should not be dereferenced or used. Technically it means that the object is in whatever state the destructor leaves it in, which for some objects may be identical to default construction (but you should never, ever rely on that).

Why they are reasonable?

Sometimes you need to destroy objects without releasing their memory. This happens in a lot of class like variant/any, various script binding and reflection system, some singleton implementations, etc.

For example, you might use std::aligned_storage to allocate a buffer for an object and then use placement new to construct an object in that buffer. You cannot call delete on this object since that will both invoke the destructor and try to free the memory backing it. You must explicitly invoke the destructor in this case to properly destruct the object.

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

There's not really such a thing as 'placement delete', other than the corresponding operator to placement new (and any calls to delete will implicitly invoke the destructor except those the compiler invokes for failed construction, e.g. your 'placement delete' notion).

One example I gave above. Another example is std::vector. You can call member functions like pop_back(). This needs to destroy the last element in the vector but it can't use delete since the memory backing the object is part of a larger buffer that must be managed separately. The same goes for many other containers, like open-addressing hash tables, deque, and so on. This is an example of where you'd want to use the template typename in order to invoke the destructor explicitly.

It's a feature that a user of a library is very rarely going to need but the implementor of a low-level library like the STL or even some application frameworks is going to need to use here and there.