简体   繁体   English

为什么 unique_ptr 将删除器作为类型参数而 shared_ptr 没有?

[英]Why does unique_ptr have the deleter as a type parameter while shared_ptr doesn't?

The std::unique_ptr template has two parameters: the type of the pointee, and the type of the deleter. std::unique_ptr模板有两个参数:指针的类型和删除器的类型。 This second parameter has a default value, so you usually just write something like std::unique_ptr<int> .第二个参数有一个默认值,所以你通常只写std::unique_ptr<int>之类的东西。

The std::shared_ptr template has only one parameter though: the type of the pointee. std::shared_ptr模板只有一个参数:指针的类型。 But you can use a custom deleter with this one too, even though the deleter type is not in the class template.但是您也可以使用自定义删除器,即使删除器类型不在类模板中。 The usual implementation uses type erasure techniques to do this.通常的实现使用类型擦除技术来做到这一点。

Is there a reason the same idea was not used for std::unique_ptr ? std::unique_ptr没有使用相同的想法是否有原因?

Part of the reason is that shared_ptr needs an explicit control block anyway for the ref count and sticking a deleter in isn't that big a deal on top. 部分原因是shared_ptr无论如何都需要一个显式的控制块用于引用计数并且保留一个删除器并不是最重要的交易。 unique_ptr however doesn't require any additional overhead, and adding it would be unpopular- it's supposed to be a zero-overhead class. 然而, unique_ptr不需要任何额外的开销,并且添加它将是不受欢迎的 - 它应该是一个零开销类。 unique_ptr is supposed to be static. unique_ptr应该是静态的。

You can always add your own type erasure on top if you want that behaviour- for example, you can have unique_ptr<T, std::function<void(T*)>> , something that I have done in the past. 如果你想要这样的行为,你总是可以在顶部添加你自己的类型擦除 - 例如,你可以拥有unique_ptr<T, std::function<void(T*)>> ,这是我过去所做的。

Another reason, in addition to the one pointed out by DeadMG, would be that it's possible to write 除了DeadMG指出的另一个原因之外,另一个原因是它可以编写

std::unique_ptr<int[]> a(new int[100]);

and ~unique_ptr will call the correct version of delete (via default_delete<_Tp[]> ) thanks to specializing for both T and T[] . ~unique_ptr将调用正确delete版本(通过default_delete<_Tp[]> ),这要归功于TT[]专用。

From C++ Primer (5th Edition)<\/strong><\/em> , Chapter 16.1.6 - Efficiency and Flexibility来自C++ Primer(第 5 版)<\/strong><\/em> ,第 16.1.6 章 - 效率和灵活性

The obvious difference between shared_ptr and unique_ptr is the strategy they use in managing the pointer they hold—one class gives us shared ownership; shared_ptr 和 unique_ptr 之间的明显区别在于它们在管理所持有的指针时使用的策略——一个类给了我们共享所有权; the other owns the pointer that it holds.另一个拥有它持有的指针。 This difference is essential to what these classes do.这种差异对于这些类的作用至关重要。

These classes also differ in how they let users override their default deleter.这些类在让用户覆盖其默认删除器的方式上也有所不同。 We can easily override the deleter of a shared_ptr by passing a callable object when we create or reset the pointer.我们可以通过在创建或重置指针时传递一个可调用对象来轻松地覆盖 shared_ptr 的删除器。 In contrast, the type of the deleter is part of the type of a unique_ptr object.相反,删除器的类型是 unique_ptr 对象类型的一部分。 Users must supply that type as an explicit template argument when they define a unique_ptr.用户在定义 unique_ptr 时必须提供该类型作为显式模板参数。 As a result, it is more complicated for users of unique_ptr to provide their own deleter.因此,unique_ptr 的用户提供自己的删除器更加复杂。

By binding the deleter at compile time, unique_ptr avoids the run-time cost of an indirect call to its deleter.<\/strong>通过在编译时绑定删除器,unique_ptr 避免了间接调用其删除器的运行时成本。<\/strong> By binding the deleter at run time, shared_ptr makes it easier for users to override the deleter.<\/strong>通过在运行时绑定删除器,shared_ptr 使用户更容易覆盖删除器。<\/strong>

<\/blockquote>"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么带有自定义删除器的unique_ptr对于nullptr不起作用,而shared_ptr呢? - Why unique_ptr with custom deleter won't work for nullptr, while shared_ptr does? 为什么std :: shared_ptr <T> = std :: unique_ptr <T[]> 编译,而std :: shared_ptr <T[]> = std :: unique_ptr <T[]> 才不是? - Why does std::shared_ptr<T> = std::unique_ptr<T[]> compile, while std::shared_ptr<T[]> = std::unique_ptr<T[]> does not? 为什么std :: unique_ptr没有像std :: shared_ptr这样的别名构造函数? - Why doesn't std::unique_ptr have an aliasing constructor like std::shared_ptr has? 将带有自定义删除器的unique_ptr移动到shared_ptr - Move a unique_ptr with custom deleter to a shared_ptr 是否有一种方便的方法可以让unique_ptr自动拥有像shared_ptr这样的删除器? - Is there a convenient way to get unique_ptr to automagically have a deleter like shared_ptr? Typedef带有静态自定义删除器的shared_ptr类型,类似于unique_ptr - Typedef a shared_ptr type with a static custom deleter, similar to unique_ptr 为什么unique_ptr不会阻止自定义删除器的切片? - Why unique_ptr doesn't prevent slicing of custom deleter? 为什么unique_ptr不能推断出删除器的类型? - Why can't unique_ptr infer the type of the deleter? 为什么unique_ptr :: reset没有带删除的重载? - Why doesn't unique_ptr::reset have overloads that take a deleter? shared_ptr的初始化 <T> 来自unique_ptr <T[]> - Initialization of shared_ptr<T> from unique_ptr<T[]>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM