intrusive_ptr在C + + 11 [英] intrusive_ptr in c++11

查看:255
本文介绍了intrusive_ptr在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()方法,另一个指向包含引用计数,并知道动态类型所拥有的对象的控制块),因此具有比侵入指针更大的足迹。

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 like boost::intrusive_ptr.

Thanks in advance.

解决方案

Does c++11 have something equivalent to 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.

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.


Note: Using make_shared allows shared_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 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.

这篇关于intrusive_ptr在C + + 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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