I've been reading about the C++11 smart pointers in order to use them on my sources, the documentation I've been reading is the one on cppreference.com; while reading about the std::unique_ptr
, on the reset
function there's a documentation that seems incorrect to me (emphasis mine):
Replaces the managed object.
Given
current_ptr
, the pointer that was managed by*this
, performs the following actions, in this order:
- Saves a copy of the current pointer
old_ptr = current_ptr
.- Overwrites the current pointer with the argument
current_ptr = ptr
.- If the old pointer was non-empty, deletes the previously managed object
if(old_ptr != nullptr) get_deleter()(old_ptr)
.
In the C++ standard documentation, we can read the well known delete null pointer feature:
Extract from n3690
standard 5.3.5 Delete (emphasis mine):
If the value of the operand of the delete-expression is not a null pointer value, then:
— If the allocation call for the new-expression for the object to be deleted was not omitted, the delete-expression shall call a deallocation function. The value returned from the allocation call of the new-expression shall be passed as the first argument to the deallocation function.
— Otherwise, the delete-expression will not call a deallocation function.
So, I'm wondering why cppreference says that the unique_ptr::reset
function checks for the nullity of the managed pointer before it's deletion even while te standard says that no deallocation function would be called over a null pointer (that's why the cppreference documentation seems incorrect to me).
Is kind of obvious that I must be mistaken and there must be a reason to do things this way, but I'm unable to imagine what reason it could be. Any hints?
PS: Where in the standard is defined how the std::unique_ptr
must be implemented or behave? In the 20.9.1 Class template unique_ptr
I cannot found anything about the check-for-nullity stuff.