从boost::shared_ptr到std: shared_ptr的转换?

[英]Conversion from boost::shared_ptr to 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。遗憾的是,这两种类型之间没有直接的转换,因为ref计数内容依赖于实现。

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共享相同的ref-count对象?或者至少从Boost版本中偷取ref-count,只让stdlib版本来处理?

2 个解决方案

#1


26  

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

您可以在std::shared_ptr“内部”携带boost::shared_ptr,使用析构函数携带引用:

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));

}

The only real reason to do this is if you have a bunch of code that expects std::shared_ptr<T>.

这样做的唯一原因是如果您有一堆期望std: shared_ptr 的代码。

#2


29  

Based on janm's response at first I did this:

根据詹姆最初的反应,我这样做了:

template<class T> std::shared_ptr<T> to_std(const boost::shared_ptr<T> &p) {
    return std::shared_ptr<T>(p.get(), [p](...) mutable { p.reset(); });
}

template<class T> boost::shared_ptr<T> to_boost(const std::shared_ptr<T> &p) {
    return boost::shared_ptr<T>(p.get(), [p](...) mutable { p.reset(); });
}

But then I realized I could do this instead:

但后来我意识到我可以这样做:

namespace {
    template<class SharedPointer> struct Holder {
        SharedPointer p;

        Holder(const SharedPointer &p) : p(p) {}
        Holder(const Holder &other) : p(other.p) {}
        Holder(Holder &&other) : p(std::move(other.p)) {}

        void operator () (...) { p.reset(); }
    };
}

template<class T> std::shared_ptr<T> to_std_ptr(const boost::shared_ptr<T> &p) {
    typedef Holder<std::shared_ptr<T>> H;
    if(H *h = boost::get_deleter<H, T>(p)) {
        return h->p;
    } else {
        return std::shared_ptr<T>(p.get(), Holder<boost::shared_ptr<T>>(p));
    }
}

template<class T> boost::shared_ptr<T> to_boost_ptr(const std::shared_ptr<T> &p){
    typedef Holder<boost::shared_ptr<T>> H;
    if(H * h = std::get_deleter<H, T>(p)) {
        return h->p;
    } else {
        return boost::shared_ptr<T>(p.get(), Holder<std::shared_ptr<T>>(p));
    }
}

This solution leaves no reason for not using it without restrictions since you get the original pointer back if you convert back to the original type.

这个解决方案没有理由不加限制地不使用它,因为如果您将原始指针转换回原始类型,那么就会得到原始指针。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/06/13/71df1a2775db812e7cfc76639e95d389.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告