Deleter类型在unique_ptr与shared_ptr中 [英] Deleter type in unique_ptr vs. shared_ptr

查看:143
本文介绍了Deleter类型在unique_ptr与shared_ptr中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发现标准定义了 std :: unique_ptr std :: shared_ptr 关于指针可能拥有的Deleter的两种完全不同的方式。以下是 cppreference :: unique_ptr cppreference :: shared_ptr

I thought it is very curious when I discovered that the standard defines std::unique_ptr and std::shared_ptr in two totally different ways regarding a Deleter that the pointer may own. Here is the declaration from cppreference::unique_ptr and 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的方式中看到:

As you can see the unique_ptr "saves" the type of the the Deleter-object as a template argument. This can also be seen in the way the Deleter is retrieved from the pointer later on:

// 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 是非成员函数?

Can someone explain the rational behind this difference? I clearly favor the concept for unique_ptr why is this not applied to shared_ptr aswell? Also, why would get_deleter be a non-member function in the latter case?

推荐答案

在这里您可以找到智能指针的原始建议: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1450.html

Here you can find the original proposal for smart pointers: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1450.html

它可以非常精确地回答您的问题:

It answers your question quite precisely:


由于删除器不是该类型的一部分,因此更改分配策略不会破坏源或二进制兼容性,并且不需要重新编译客户端。

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 具有更大的灵活性,例如,具有不同删除器的 shared_ptr 实例可以存储在同一容器中。

This is also useful because gives the clients of std::shared_ptr some more flexibility, for example shared_ptr instances with different deleters can be stored in the same container.

此外,因为 shared_ptr 实现需要一个共享内存块(用于存储引用计数),并且因为与原始指针相比,alreay必须有一些开销,在这里添加类型擦除的删除器没什么大不了的。

Also, because the shared_ptr implementations needs a shared memory block anyhow (for storing the reference count) and because there alreay has to be some overhead compared to raw pointers, adding a type-erased deleter is not much of a big deal here.

unique_ptr 的目的是完全没有开销,每个实例都必须嵌入其删除器,因此将其作为类型的一部分是很自然的事情。

unique_ptr on the other hand are inteded to have no overhead at all and every instance has to embed its deleter, so making it a part of the type is the natural thing to do.

这篇关于Deleter类型在unique_ptr与shared_ptr中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆