intrusive_ptr在C + + 11 [英] intrusive_ptr in c++11
问题描述
请问C ++ 11不得不 intrusive_ptr
等价的东西的boost ::?
我的问题是,我在我的C ++ code A C风格的界面。因此,接口两侧可以使用C ++,但暴露C接口所需的兼容性的原因。我不能使用的std :: shared_ptr的
,因为我有过两个(或更多)智能指针来管理的对象。我无法弄清楚的东西,如的boost :: intrusive_ptr
的解决方案。
先谢谢了。
请问C ++ 11具有等同的东西来提高:: intrusive_ptr?
块引用>没有。
它有
的std :: make_shared
这意味着的std :: shared_ptr的
差不多的(见注下文)的同样有效的侵入智能指针,因为该参考计数将被存储在存储器中的对象本身相邻的,改进的参考和缓存的使用地点。它还提供了的std :: enable_shared_from_this
,它允许您检索的std :: shared_ptr的
当你只有一个内置在指针由的shared_ptr
拥有的对象,但是,这并不让你使用不同的智能指针类型来管理的对象。
的shared_ptr
预计将作为管理对象全部责任。不同的智能指针类型可能只管理强引用计数,而不是弱引用计数,这将使数以不同步,打破的shared_ptr
的不变量注意:使用
make_shared
允许的shared_ptr
是的 的有效作为侵入几乎一样指针。对象和引用计数信息可以在一个单独的内存块被分配当使用make_shared
,但仍然会有两个参考计数(对于强和弱计数),这是不是侵入指针的情况,因为他们不支持的weak_ptr
。此外,的shared_ptr
对象本身总是具有存储两个指针(即由的shared_ptr :: get()方法$ C $返回一个C>,另一个指向包含引用计数,并知道动态类型所拥有的对象的控制块),因此具有比侵入指针更大的足迹。
Does c++11 have something equivalent to
boost::intrusive_ptr
?My problem is that I have a C style interface over my c++ code. So, both sides of interface can use c++ but exposing C interface is required for compatibility reasons. I can not use
std::shared_ptr
since I have to manage the object through two (or more) smart pointers. I am unable to figure out a solution with something likeboost::intrusive_ptr
.Thanks in advance.
解决方案Does c++11 have something equivalent to boost::intrusive_ptr?
No.
It does have
std::make_shared
which meansstd::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 providesstd::enable_shared_from_this
which allows you to retrieve astd::shared_ptr
when you only have a built-in pointer to an object owned by ashared_ptr
, but that doesn't allow you to manage the object using different smart pointer types.
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 ofshared_ptr
.
Note: Using
make_shared
allowsshared_ptr
to be almost as efficient as an intrusive pointer. The object and the reference counting info can be allocated in a single chunk of memory whenmake_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 aweak_ptr
. Also, theshared_ptr
object itself always has to store two pointers (the one that will be returned byshared_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.这篇关于intrusive_ptr在C + + 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!