C++20 introduces the concept of a "destroying operator delete", as described below:
delete-expressions does not execute the destructor for *p before placing a call to operator delete
So, given the following struct S
:
struct S {
void operator delete(S* p, std::destroying_delete_t);
private:
~S();
};
I'd expect the delete
below to not insert a call to destructor but just call destroying operator delete we provided
delete new S;
However, GCC/Clang/MSVC behave differently: DEMO
Only GCC doesn't try to access ~S()
, others still require ~S()
being accessible.
Which one is correct?
The answer is yes. Destructor for each object is called. On a related note, you should try to avoid using delete whenever possible.
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.
So delete is for managing the dynamic memory, but the destructor is a method of the class itself, which is always called when the object is getting freed from the memory (stack or heap).
A destructor only destroys and cleans up the object. It does not deallocate the memory.
gcc is correct: ~S()
need not be accessible.
From [expr.delete]/6:
If the value of the operand of the delete-expression is not a null pointer value and the selected deallocation function (see below) is not a destroying operator delete, the delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted.
It's only in the case of not destroying delete that the destructor is invoked. Indeed, that's the whole point of destroying delete - to give the class author control of how/when to invoke the destructor. As such, there is no requirement that the destructor be accessible - it's not up to the language to invoke it, it's up to the user.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With