c++11 中的 intrusive_ptr
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13912286/
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
intrusive_ptr in c++11
提问by Aarkan
Does C++11 have something equivalent to boost::intrusive_ptr
?
C++11 有什么等价于boost::intrusive_ptr
吗?
My problem is that I have a C-style interface over my C++ code. Both sides of the interface can use C++, but exposing the C interface is required for compatibility reasons. I cannot use std::shared_ptr
because I have to manage the object through two (or more) smart pointers. I am unable to figure out a solution with something like boost::intrusive_ptr
.
我的问题是我的 C++ 代码有一个 C 风格的界面。接口的两边都可以使用C++,但是出于兼容性的考虑,需要暴露C接口。我无法使用,std::shared_ptr
因为我必须通过两个(或更多)智能指针来管理对象。我无法找到类似boost::intrusive_ptr
.
回答by Jonathan Wakely
Does c++11 have something equivalent to boost::intrusive_ptr?
c++11 有什么等同于 boost::intrusive_ptr 的东西吗?
No.
不。
It does have std::make_shared
which means std::shared_ptr
is almost (see note below)as efficient as an intrusive smart pointer, because the reference counts will be stored adjacent in memory to the object itself, improving locality of reference and cache usage. It also provides std::enable_shared_from_this
which allows you to retrieve a std::shared_ptr
when you only have a built-in pointer to an object owned by a shared_ptr
, but that doesn't allow you to manage the object using different smart pointer types.
它确实有std::make_shared
该装置std::shared_ptr
几乎是(参见下文注释)一样有效侵入式智能指针,这是因为参考计数将被存储在相邻存储器对象本身,提高的参考和高速缓存使用局部性。当您只有一个指向 a 拥有的对象的内置指针时,它还提供了std::enable_shared_from_this
允许您检索 astd::shared_ptr
的功能shared_ptr
,但这不允许您使用不同的智能指针类型来管理该对象。
shared_ptr
expects to be entirely responsible for managing the object. A different smart pointer type might only manage the "strong" refcount and not the "weak" refcount, which would allow the counts to get out of sync and break the invariants of shared_ptr
.
shared_ptr
期望完全负责管理对象。不同的智能指针类型可能只管理“强”引用计数而不是“弱”引用计数,这将允许计数不同步并破坏shared_ptr
.
Note: Using make_shared
allows shared_ptr
to be almostas efficient as an intrusive pointer. The object and the reference counting info can be allocated in a single chunk of memory when make_shared
is used, but there will still be two reference counts (for the "strong" and "weak" counts) which isn't the case for intrusive pointers as they don't support a weak_ptr
. Also, the shared_ptr
object itself always has to store two pointers (the one that will be returned by shared_ptr::get()
and another pointer to the "control block" that contains the reference counts and knows the dynamic type of the owned object) so has a larger footprint than an intrusive pointer.
注意:使用make_shared
允许shared_ptr
将几乎为侵入指针有效。对象和引用计数信息可以在使用时分配在单个内存块中make_shared
,但仍然会有两个引用计数(对于“强”和“弱”计数),这对于侵入式指针而言并非如此他们不支持weak_ptr
. 此外,shared_ptr
对象本身总是必须存储两个指针(一个将被返回shared_ptr::get()
,另一个指向包含引用计数并知道所拥有对象的动态类型的“控制块”的指针),因此比侵入性指针。