从boost :: shared_ptr的转换到std :: shared_ptr的? [英] Conversion from boost::shared_ptr to std::shared_ptr?

查看:929
本文介绍了从boost :: shared_ptr的转换到std :: shared_ptr的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的内部使用的shared_ptr 的Boost的版本,并公开只有那些库。我的应用程序,我想使用的std :: shared_ptr的尽可能虽然。可悲的是,这两种类型之间没有直接转换,裁判计数东西是依赖于实现的。

I got a library that internally uses Boost's version of shared_ptr and exposes only those. For my application, I'd like to use std::shared_ptr whenever possible though. Sadly, there is no direct conversion between the two types, as the ref counting stuff is implementation dependent.

有没有办法同时拥有的boost :: shared_ptr的的std :: shared_ptr的共享相同裁判计数对象?或至少​​来自Boost版偷裁判计数,只有让STDLIB版本照顾它?

Is there any way to have both a boost::shared_ptr and a std::shared_ptr share the same ref-count-object? Or at least steal the ref-count from the Boost version and only let the stdlib version take care of it?

推荐答案

您可以通过使用析构函数随身携带的基准进行了boost :: shared_ptr的内部的std :: shared_ptr的:

You can carry the boost::shared_ptr "inside" the std::shared_ptr by using the destructor to carry the reference around:

template<typename T>
void do_release(typename boost::shared_ptr<T> const&, T*)
{
}

template<typename T>
typename std::shared_ptr<T> to_std(typename boost::shared_ptr<T> const& p)
{
    return
        std::shared_ptr<T>(
                p.get(),
                boost::bind(&do_release<T>, p, _1));

}

唯一的真正原因,要做到这一点,如果你有一堆code的一个期望的std :: shared_ptr的&LT; T&GT;

这篇关于从boost :: shared_ptr的转换到std :: shared_ptr的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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