The C++20 standard says (see [expr.delete])
If the value of the operand of the delete-expression is a null pointer value, it is unspecified whether a deallocation function will be called as described above.
And cppreference.com says (see delete expression)
If expression evaluates to a null pointer value, no destructors are called, and the deallocation function may or may not be called (it's unspecified), but the default deallocation functions are guaranteed to do nothing when passed a null pointer.
Why would a compiler call a deallocation function, if the operand of the delete-expression is null?
The rule would be simpler if it would be guaranteed, that a deallocation function would not be called, but I suppose the standard allows this for a good reason.
PROPOSAL: In my opinion delete ptr;
should be specified as equivalent to this:
if (ptr) {
// call dtor.
// call deallocation function
}
If ptr
is known to be null or not null, we optimize accordingly. The deallocation functions would assume that their argument is not null, therefore no double != nullptr
check would happen.
I think this is simpler.