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.
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?
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:
The only real reason to do this is if you have a bunch of code that expects
std::shared_ptr<T>.