Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Point out a possible issue with weak_ptr lifetimes potentially extending deleter lifetimes.
Source Link
janm

Update to an "off the top of my head" answer, almost eleven years later:

As pointed out in this answer, an implementation is allowed to extend the lifetime the deleter beyond the lifetime of the shared_ptr. For example, until after all weak_ptr instances are also destroyed. This would cause a problem where the presence of weak_ptr instances prevent the destruction of the underlying object, obviously a problem.

To avoid this, either use the approach in the answer by @Fozi with an explicit call to reset(), or the aliasing constructor approach in the linked answer.

Original Answer:

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

}

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

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

}

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

Update to an "off the top of my head" answer, almost eleven years later:

As pointed out in this answer, an implementation is allowed to extend the lifetime the deleter beyond the lifetime of the shared_ptr. For example, until after all weak_ptr instances are also destroyed. This would cause a problem where the presence of weak_ptr instances prevent the destruction of the underlying object, obviously a problem.

To avoid this, either use the approach in the answer by @Fozi with an explicit call to reset(), or the aliasing constructor approach in the linked answer.

Original Answer:

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

}

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

Bugfix: namespace of the returned shared_ptr was wrong, thanks Tomalak Geret'kal
Source Link
janm

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
        booststd::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>.

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

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

}

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

Source Link
janm

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

default