In C++11 std::shared_ptr
has four constructors which can be passed deleter objects d
of type D
. The signatures of these constructors are the following:
template<class Y, class D> shared_ptr(Y * p, D d);
template<class Y, class D, class A> shared_ptr(Y * p, D d, A a);
template <class D> shared_ptr(nullptr_t p, D d);
template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
The standard requires in [util.smartptr.shared.const] type D
to be CopyConstructible. Why is this needed? If shared_ptr
makes copies of d
then which of these deleters might get called? Wouldn't it possible for a shared_ptr
only to keep a single deleter around? What does it mean for a shared_ptr
to own a deleter if d
can be copied?
What is the rationale behind the CopyConstructible requirement?
PS: This requirement might complicate writing deleters for shared_ptr
. unique_ptr
seems to have much better requirements for its deleter.
question from:
https://stackoverflow.com/questions/36744238/why-do-shared-ptr-deleters-have-to-be-copyconstructible 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…