Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-12-10 07:33:00


From: "Hickman, Greg" <greg.hickman_at_[hidden]>
> I notice that a shared_ptr can be constructed directly from an
> intrusive_ptr, ...

This is no longer the case with the current version. The implicit conversion
has been removed.

> ... but a weak_ptr cannot. Is there some reason why we wouldn't
> want to add the appropriate constructor to weak_ptr?

weak_ptr doesn't play well with intrusive counted objects. It is supposed to
keep the count alive, but not affect the object's lifetime. When the count
is embedded within the object, weak_ptr keeps the whole object alive. This
is confusing.

The currently blessed way to get a shared_ptr from an intrusive_ptr is to
use a custom deleter:

template<class T> struct intrusive_deleter
{
    void operator()(T * p)
    {
        if(p) intrusive_ptr_release(p);
    }
};

shared_ptr<X> make_shared_from_intrusive(X * p)
{
    if(p) intrusive_ptr_add_ref(p);
    shared_ptr<X> px(p, intrusive_deleter<X>());
    return px;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk