智能指针(提升)解释 [英] smart pointers (boost) explained

查看:29
本文介绍了智能指针(提升)解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面这组指针有什么区别?你什么时候在生产代码中使用每个指针(如果有的话)?

What is the difference between the following set of pointers? When do you use each pointer in production code, if at all?

示例将不胜感激!

  1. scoped_ptr

shared_ptr

weak_ptr

intrusive_ptr

您在生产代码中使用 boost 吗?

Do you use boost in production code?

推荐答案

智能指针的基本属性

当您拥有可以分配每个智能指针的属性时,这很容易.有三个重要的属性.

Basic properties of smart pointers

It's easy when you have properties that you can assign each smart pointer. There are three important properties.

  • 完全没有所有权
  • 所有权转让
  • 所有权份额

第一个意思是智能指针不能删除对象,因为它不拥有它.第二种意味着只有一个智能指针可以同时指向同一个对象.例如,如果要从函数返回智能指针,则所有权将转移到返回的智能指针.

The first means that a smart pointer cannot delete the object, because it doesn't own it. The second means that only one smart pointer can ever point to the same object at the same time. If the smart pointer is to be returned from functions, the ownership is transferred to the returned smart pointer, for example.

第三个意思是多个智能指针可以同时指向同一个对象.这也适用于原始指针,但是原始指针缺少一个重要的特性:它们没有定义它们是否拥有.如果每个所有者都放弃该对象,则所有权共享智能指针将删除该对象.这种行为恰好经常需要,因此共享拥有智能指针被广泛传播.

The third means that multiple smart pointers can point to the same object at the same time. This applies to a raw pointer too, however raw pointers lack an important feature: They do not define whether they are owning or not. A share of ownership smart pointer will delete the object if every owner gives up the object. This behavior happens to be needed often, so shared owning smart pointers are widely spread.

一些拥有智能指针既不支持第二个也不支持第三个.因此它们不能从函数返回或传递到其他地方.这最适合 RAII 目的,其中智能指针保留在本地并刚刚创建,以便在超出范围后释放对象.

Some owning smart pointers support neither the second nor the third. They can therefore not be returned from functions or passed somewhere else. Which is most suitable for RAII purposes where the smart pointer is kept local and is just created so it frees an object after it goes out of scope.

所有权共享可以通过复制构造函数来实现.这自然会复制一个智能指针,并且副本和原始指针都将引用同一个对象.所有权的转移目前无法在 C++ 中真正实现,因为没有办法将某些东西从一个对象转移到该语言支持的另一个对象:如果您尝试从函数返回一个对象,则发生的情况是该对象被复制.因此,实现所有权转移的智能指针必须使用复制构造函数来实现所有权转移.然而,这反过来又破坏了它在容器中的使用,因为需求声明了容器元素的复制构造函数的某些行为,这与这些智能指针的这种所谓的移动构造函数"行为不兼容.

Share of ownership can be implemented by having a copy constructor. This naturally copies a smart pointer and both the copy and the original will reference the same object. Transfer of ownership cannot really be implemented in C++ currently, because there are no means to transfer something from one object to another supported by the language: If you try to return an object from a function, what is happening is that the object is copied. So a smart pointer that implements transfer of ownership has to use the copy constructor to implement that transfer of ownership. However, this in turn breaks its usage in containers, because requirements state a certain behavior of the copy constructor of elements of containers which is incompatible with this so-called "moving constructor" behavior of these smart pointers.

C++1x 通过引入所谓的移动构造函数"和移动赋值运算符"为所有权转移提供了本机支持.它还带有这样一个名为 unique_ptr 的所有权转移智能指针.

C++1x provides native support for transfer-of-ownership by introducing so-called "move constructors" and "move assignment operators". It also comes with such a transfer-of-ownership smart pointer called unique_ptr.

scoped_ptr 是一个智能指针,既不可转移也不可共享.如果您在本地需要分配内存,则它仅可用,但请确保在超出范围时再次释放它.但如果您愿意,它仍然可以与另一个 scoped_ptr 交换.

scoped_ptr is a smart pointer that is neither transferable nor sharable. It's just usable if you locally need to allocate memory, but be sure it's freed again when it goes out of scope. But it can still be swapped with another scoped_ptr, if you wish to do so.

shared_ptr 是一个共享所有权的智能指针(上面的第三种).它是引用计数的,因此它可以看到它的最后一个副本何时超出范围,然后释放所管理的对象.

shared_ptr is a smart pointer that shares ownership (third kind above). It is reference counted so it can see when the last copy of it goes out of scope and then it frees the object managed.

weak_ptr 是一个非拥有的智能指针.它用于在不添加引用计数的情况下引用托管对象(由 shared_ptr 管理).通常,您需要从 shared_ptr 中获取原始指针并将其复制.但这并不安全,因为您无法检查对象何时被实际删除.因此,weak_ptr 通过引用由 shared_ptr 管理的对象来提供方法.如果您需要访问该对象,您可以锁定它的管理(以避免在另一个线程中使用该对象时 shared_ptr 释放它)然后使用它.如果weak_ptr 指向一个已经删除的对象,它会通过抛出异常来通知你.当您有循环引用时,使用weak_ptr 是最有益的:引用计数无法轻松应对这种情况.

weak_ptr is a non-owning smart pointer. It is used to reference a managed object (managed by a shared_ptr) without adding a reference count. Normally, you would need to get the raw pointer out of the shared_ptr and copy that around. But that would not be safe, as you would not have a way to check when the object was actually deleted. So, weak_ptr provides means by referencing an object managed by shared_ptr. If you need to access the object, you can lock the management of it (to avoid that in another thread a shared_ptr frees it while you use the object) and then use it. If the weak_ptr points to an object already deleted, it will notice you by throwing an exception. Using weak_ptr is most beneficial when you have a cyclic reference: Reference counting cannot easily cope with such a situation.

intrusive_ptr 类似于 shared_ptr,但它不会将引用计数保留在 shared_ptr 中,而是将计数的递增/递减留给一些需要由管理的对象定义的辅助函数.这样做的好处是可以将已经被引用的对象(其引用计数由外部引用计数机制递增)填充到 intrusive_ptr 中——因为引用计数不再是智能指针的内部,但智能指针使用现有的引用计数机制.

intrusive_ptr is like a shared_ptr but it does not keep the reference count in a shared_ptr but leaves incrementing/decrementing the count to some helper functions that need to be defined by the object that is managed. This has the advantage that an already referenced object (which has a reference count incremented by an external reference counting mechanism) can be stuffed into an intrusive_ptr - because the reference count is not anymore internal to the smart pointer, but the smart pointer uses an existing reference counting mechanism.

unique_ptr 是所有权指针的转移.你不能复制它,但你可以使用 C++1x 的移动构造函数来移动它:

unique_ptr is a transfer of ownership pointer. You cannot copy it, but you can move it by using C++1x's move constructors:

unique_ptr<type> p(new type);
unique_ptr<type> q(p); // not legal!
unique_ptr<type> r(move(p)); // legal. p is now empty, but r owns the object
unique_ptr<type> s(function_returning_a_unique_ptr()); // legal!

这是 std::auto_ptr 遵循的语义,但由于缺少对移动的原生支持,它无法毫无缺陷地提供它们.unique_ptr 将自动从临时的其他 unique_ptr 中窃取资源,这是移动语义的关键特征之一.auto_ptr 将在下一个 C++ 标准版本中被弃用,以支持 unique_ptr.C++1x 还允许填充只能移动但不能复制到容器中的对象.例如,您可以将 unique_ptr 填充到向量中.我会在这里停下来向您介绍 a如果你想阅读更多关于这个的好文章.

This is the semantic that std::auto_ptr obeys, but because of missing native support for moving, it fails to provide them without pitfalls. unique_ptr will automatically steal resources from a temporary other unique_ptr which is one of the key features of move semantics. auto_ptr will be deprecated in the next C++ Standard release in favor of unique_ptr. C++1x will also allow stuffing objects that are only movable but not copyable into containers. So you can stuff unique_ptr's into a vector for example. I'll stop here and reference you to a fine article about this if you want to read more about this.

这篇关于智能指针(提升)解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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