C++ shared_ptr 与 scoped_ptr

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1770636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:08:17  来源:igfitidea点击:

shared_ptr vs scoped_ptr

c++boostshared-ptrsmart-pointersscoped-ptr

提问by Narek

scoped_ptris not copy able and is being deleted out of the scope. So it is kind of restricted shared_ptr. So seems besides the cases when you really need to restrict the copy operation shared_ptris better to use. Because sometimes you don't know you need to create a copy of your object or no. So the question is: besides the cases mentioned above, could we consider that shared_ptris better (or recommended) to use instead of scoped_ptr. Does scoped_ptrwork much faster from shared_ptr, or does it have any advantages?

scoped_ptr不能复制并且正在被删除到范围之外。所以它有点受限制shared_ptr。因此,除了您确实需要限制复制操作的情况之外,似乎shared_ptr最好使用。因为有时您不知道是否需要创建对象的副本。所以问题是:除了上面提到的情况之外,我们shared_ptr是否可以认为使用scoped_ptr. 是否scoped_ptr工作得更快shared_ptr,或者它有什么优势?

Thanks!

谢谢!

回答by Mike Seymour

shared_ptris more heavyweight than scoped_ptr. It needs to allocate and free a reference count object as well as the managed object, and to handle thread-safe reference counting - on one platform I worked on, this was a significant overhead.

shared_ptrscoped_ptr. 它需要分配和释放引用计数对象以及托管对象,并处理线程安全的引用计数——在我工作的一个平台上,这是一个很大的开销。

My advice (in general) is to use the simplest object that meets your needs. If you need reference-counted sharing, use shared_ptr; if you just need automatic deletion once you've finished with a single reference, use scoped_ptr.

我的建议(一般而言)是使用满足您需求的最简单的对象。如果您需要引用计数共享,请使用shared_ptr; 如果您只需要在完成单个引用后自动删除,请使用scoped_ptr.

回答by peterchen

Performance- shared_ptrhas more functionality, but also requires an additional allocation (it's also larger, but that rarely matters).

性能-shared_ptr具有更多功能,但也需要额外的分配(它也更大,但这很少重要)。

[edit] The second allocation can be avoided by using make_shared, but then weak_ptrwill hold the entire entire allocation even after the object is destroyed, which may be a problem for large objects.

[编辑] 使用 可以避免第二次分配make_shared,但weak_ptr即使在对象被销毁后也会保留整个分配,这对于大对象可能是一个问题。

Expresison of Intentusing scoped_ptryou state more explicitly what you want to do. (In case you wonder - that's a good thing :) ). If you do this correctly, shared_ptr will also indicate "this object is intended to live beyond this scope"

使用scoped_ptr的意图表达更明确地说明您想要做什么。(如果你想知道 - 这是一件好事:))。如果您正确执行此操作, shared_ptr 还将指示“此对象旨在超出此范围”

回答by sellibitze

Their intended purpose is different, so, in many cases "shared_ptr vs scoped_ptr" is not a question at all. Sure, you can use a shared_ptr when all you need is a scoped_ptr. But what's the point? shared_ptr has likely a slightly bigger overhead with all the reference counting involved.

它们的预期目的不同,因此,在许多情况下,“shared_ptr 与 scoped_ptr”根本不是问题。当然,当您只需要一个 scoped_ptr 时,您可以使用 shared_ptr。但重点是什么?shared_ptr 可能会稍微增加一些涉及所有引用计数的开销。

回答by Alexey Malistov

scoped_ptrworks much faster from shared_ptr. It's right. shared_ptralways allocate memory using your allocator or default allocator.

scoped_ptrshared_ptr. 这是正确的。shared_ptr始终使用您的分配器或默认分配器分配内存。

回答by Zack Yezek

Scoped_ptr has little in common with shared_ptr, weak_ptr, or unique_ptr because it is only doing very special case of "reference counting". It isn't something you will need very often in well-designed code, but it is a good tool to have available.

Scoped_ptr 与 shared_ptr、weak_ptr 或 unique_ptr 几乎没有共同之处,因为它只执行“引用计数”的非常特殊情况。在精心设计的代码中,它不是您经常需要的东西,但它是一个可用的好工具。

Basically, a scoped_ptr isn't a reference-counted thing at all. Rather, it is an object you create on the stack (within the local scope) so that you can do something like this:

基本上, scoped_ptr 根本不是引用计数的东西。相反,它是您在堆栈上(在本地范围内)创建的一个对象,以便您可以执行以下操作:

  //Some enclosing scope- anything set off by "{}" or even a function:
   {
       scoped_ptr<MyObject> ptr = new MyObject( parameters...);
   } // When we hit this closing brace, "ptr" will delete the "MyObject" inside.

You tend to see this pattern more with mutexes and other synchronization primatives- I can declare an "AutoLock" that will lock the mutex passed into it, then unlock it when it deletes to turn the whole "{}" scope into a critical section.

您倾向于在互斥锁和其他同步原语中更多地看到这种模式——我可以声明一个“AutoLock”来锁定传递给它的互斥锁,然后在它删除时解锁它以将整个“{}”范围变成临界区。

Also notice that a 'scoped_ptr' only ever makes sense when you can't just do a plain-old stack allocation like "MyObject obj(params..)" for some reason. After all, what it is doing is letting you use a heap-allocated object as if it was one on the stack. That tends to be a lot rarer a use case than the reference-counting of shared_ptr & its cousins.

还要注意,“scoped_ptr”只有在由于某种原因不能像“MyObject obj(params..)”这样的普通堆栈分配时才有意义。毕竟,它所做的是让您使用堆分配的对象,就好像它是堆栈中的对象一样。这往往比 shared_ptr 及其表亲的引用计数更罕见。