\" operator. Is there some rationale behind weak_ptr not having the same functional" />
开发者

Can I get a raw pointer from boost's weak_ptr?

开发者 https://www.devze.com 2022-12-31 02:53 出处:网络
Is it possible to get a raw pointer from boost:开发者_如何学C:weak_ptr? Boost\'s shared_ptr has get() method and \"->\" operator. Is there some rationale behind weak_ptr not having the same functional

Is it possible to get a raw pointer from boost:开发者_如何学C:weak_ptr? Boost's shared_ptr has get() method and "->" operator. Is there some rationale behind weak_ptr not having the same functionality?


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

精彩评论

暂无评论...
验证码 换一张
取 消