Is it possible to get a raw pointer from boost::weak_ptr? Boost's shared_ptr has get() method and "->" operator. Is there some rationale behind weak_ptr not having the same functionality?
3 Answers
A weak_ptr
holds a non-owning reference, so the object to which it refers may not exist anymore. It would be inherently dangerous to use a raw pointer held by a weak_ptr
.
The correct approach is to promote the weak_ptr
to a shared_ptr
using weak_ptr::lock()
and get the pointer from that.
The Boost weak_ptr
documentation explains why it would be unsafe to provide get()
functionality as part of weak_ptr
, and has examples of code that can cause problems.
This is an old question and the accepted answer is good, so I hesitate to post another answer, but one thing that seems missing is a good idiomatic usage example:
boost::weak_ptr<T> weak_example;
...
if (boost::shared_ptr<T> example = weak_example.lock())
{
// do something with example; it's safe to use example.get() to get the
// raw pointer, *only if* it's only used within this scope, not cached.
}
else
{
// do something sensible (often nothing) if the object's already destroyed
}
A key advantage of this idiom is that the strong pointer is scoped to the if-true block, which helps to prevent accidental use of a non-initialised reference, or keeping a strong reference around longer than it is actually required.
You first need to derive the shared_ptr from the weak_ptr before getting hold of the raw pointer.
You can call lock to get the shared_ptr, or the shared_ptr constructor:
boost::weak_ptr<int> example;
...
int* raw = boost::shared_ptr<int>(example).get();