Boost logo

Boost :

Subject: Re: [boost] enable_shared_from_this2
From: Berserker (berserker_r_at_[hidden])
Date: 2009-10-28 05:08:57


> I get it. I haven't read Berserker's tests carefully (sorry), and our
> tests do indeed not cover this case. Either ~enable_shared_from_this2
> should be made virtual and (T*)0 be made a dynamic_cast<T*>(this) as in
> the original, or shared_from_this should be a free function, as you
> suggest. (How about 'shared_from_raw' as a name for the free function?)

Thanks for yours replies, I hope that you'll find the time to have a
look at my test :)
Peter did you remember the problem related to boost::python I'm talking
about? Here is the link of your reply
http://lists.boost.org/boost-users/2008/08/38864.php
I'm experiencing that problem and the only way I can solve it is using
the patch in my first message.

I'll "suggest" two new methods in enable_shared_from_this2 too:

template <typename I>
inline shared_ptr<I> shared_from_this()
{
    return boost::dynamic_pointer_cast<I>(shared_from_this());
}

template <typename I>
inline shared_ptr<I const> shared_from_this() const
{
    return boost::dynamic_pointer_cast<I const>(shared_from_this());
}

This is for sure nothing special, but I think it's very useful in case
of overriding from base classes which inherit from
enable_shared_from_this2, for example:

class A : public enable_shared_from_this2<A>
{

};

class B : public A
{
    void foo1()
    {
       // short version :)
       shared_ptr<B> me = shared_from_this<B>();
    }

    void foo2()
    {
       // verbose version :(
       shared_ptr<B> me =
boost::dynamic_pointer_cast<B>(shared_from_this());
    }
};


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