calling destructor explicitly

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.

Leave a Comment