最佳答案
Both unique_ptr
and shared_ptr
accept a custom deleter to call on the object they own. But in the case of unique_ptr
, the deleter is passed as a template parameter of the class, whereas the type of shared_ptr
's custom deleter is to be specified as a template parameter of the constructor.
template <class T, class D = default_delete<T>>
class unique_ptr
{
unique_ptr(T*, D&); //simplified
...
};
and
template<class T>
class shared_ptr
{
template<typename D>
shared_ptr(T*, D); //simplified
...
};
I can't see why such difference. What requires that?