std::default_delete
can be specialized to allow std::unique_ptr
s to painlessly manage types which have to be destroyed by calling some custom destroy-function instead of using delete p;
.
There are basically two ways to make sure an object is managed by a std::shared_ptr
in C++:
Create it managed by a shared-pointer, using
std::make_shared
orstd::allocate_shared
. This is the preferred way, as it coalesces both memory-blocks needed (payload and reference-counts) into one. Though iff there are onlystd::weak_ptr
s left, the need for the reference-counts will by necessity still pin down the memory for the payload too.Assign management to a shared-pointer afterwards, using a constructor or
.reset()
.
The second case, when not providing a custom deleter is interesting:
Specifically, it is defined to use its own deleter of unspecified type which uses delete [] p;
or delete p;
respectively, depending on the std::shared_ptr
being instantiated for an Array or not.
Quote from n4659 (~C++17):
template<class Y> explicit shared_ptr(Y* p);
4 Requires:
Y
shall be a complete type. The expressiondelete[] p
, whenT
is an array type, ordelete p
, whenT
is not an array type, shall have well-defined behavior, and shall not throw exceptions.
5 Effects: WhenT
is not an Array type, constructs ashared_ptr
object that owns the pointerp
. Otherwise, constructs ashared_ptr
that ownsp
and a deleter of an unspecified type that callsdelete[] p
. WhenT
is not an array type, enablesshared_from_this
withp
. If an exception is thrown,delete p
is called whenT
is not an array type,delete[] p
otherwise.
6 Postconditions:use_count() == 1 && get() == p
.
[…]template<class Y> void reset(Y* p);
3 Effects: Equivalent to
shared_ptr(p).swap(*this)
.
My questions are:
- Is there a, preferably good, reason that it is not specified to use
std::default_delete
instead? - Would any valid (and potentially useful?) code be broken by that change?
- Is there already a proposal to do so?
delete
(ordelete[]
). I don't see how you can legally specialize it to do something else. – Judejudeastd::make_shared
orstd::allocate_shared
coalesce both allocations. That's only background for the question. – Appulse