当我发现标准以两种完全不同的方式定义了 std::unique_ptr
和 std::shared_ptr
时,我觉得这非常奇怪可能拥有。这是来自 cppreference::unique_ptr 的声明和 cppreference::shared_ptr :
template<
class T,
class Deleter = std::default_delete<T>
> class unique_ptr;
template< class T > class shared_ptr;
如您所见,unique_ptr 将 Deleter 对象的类型“保存”为模板参数。这也可以从稍后从指针中检索 Deleter 的方式中看出:
// unique_ptr has a member function to retrieve the Deleter
template<
class T,
class Deleter = std::default_delete<T>
>
Deleter& unique_ptr<T, Deleter>::get_deleter();
// For shared_ptr this is not a member function
template<class Deleter, class T>
Deleter* get_deleter(const std::shared_ptr<T>& p);
有人可以解释这种差异背后的原因吗?我显然赞成 unique_ptr
的概念,为什么这也不适用于 shared_ptr
?另外,为什么 get_deleter
在后一种情况下会是一个非成员函数?
最佳答案
您可以在此处找到智能指针的原始提案:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1450.html
它非常准确地回答了你的问题:
Since the deleter is not part of the type, changing the allocation strategy does not break source or binary compatibility, and does not require a client recompilation.
这也很有用,因为为 std::shared_ptr
的客户端提供了更多的灵 active ,例如具有不同删除器的 shared_ptr
实例可以存储在同一个容器中。
另外,因为 shared_ptr
实现无论如何都需要一个共享内存块(用于存储引用计数)并且因为与原始指针相比已经有一些开销,所以添加一个类型删除删除器是这里没什么大不了的。
unique_ptr
旨在完全没有开销,并且每个实例都必须嵌入其删除器,因此将其作为类型的一部分是很自然的事情。
关于c++ - unique_ptr 与 shared_ptr 中的删除器类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27742290/