Cohabitation of boost::shared_ptr and std::shared_ptr

前端 未结 1 590
轻奢々
轻奢々 2020-12-12 13:28

I want to use boost::log at some point, but I cannot pass a std::shared_ptr as a parameter, because the compiler (VS2010) cannot convert it into a

相关标签:
1条回答
  • 2020-12-12 14:00

    You could do it like this:

    template<typename T>
    boost::shared_ptr<T> make_shared_ptr(std::shared_ptr<T>& ptr)
    {
        return boost::shared_ptr<T>(ptr.get(), [ptr](T*) mutable {ptr.reset();});
    }
    
    template<typename T>
    std::shared_ptr<T> make_shared_ptr(boost::shared_ptr<T>& ptr)
    {
        return std::shared_ptr<T>(ptr.get(), [ptr](T*) mutable {ptr.reset();});
    }
    

    EDIT: Note that this does not work with weak references to the source ptr. So be careful with those!

    0 讨论(0)
提交回复
热议问题