Conversion from boost::shared_ptr to std::shared_ptr?

后端 未结 2 1198
醉酒成梦
醉酒成梦 2020-12-04 15:37

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 wh

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 16:11

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

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

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

提交回复
热议问题