Boost logo

Boost :

Subject: [boost] [shared_ptr] Two suggestions for the aliasing constructor
From: Clive Jones (clive-boost_at_[hidden])
Date: 2008-12-06 09:07:57


1) An aliasing constructor that takes an rvalue reference

Motivating example: in my code I have a utility function...

template <typename T, typename Y>
boost::shared_ptr<T> shared_ptr_to_member(const boost::shared_ptr<Y> &ptr,
                                          T Y::*ptr_to_member)
{
    return boost::shared_ptr<T>(ptr, &(ptr.get()->*ptr_to_member));
}

...which seems to me a prime candidate for having an overload that
takes an rvalue reference.

The implementation would be very simple, by direct analogy with the
existing aliasing constructor and rvalue constructor.

    template< class Y >
    shared_ptr( shared_ptr<Y> && r, T * p ): px( p ), pn() // never throws
    {
        pn.swap( r.pn );
        r.px = 0;
    }

2) Refactor the pointer_cast functions to use the aliasing constructor

The pointer casting functions were implemented before the aliasing
constructor was available; using it, simpler implementation is
possible. For example, rewriting static_pointer_cast as:

    template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r)
    {
        return shared_ptr<T>(r, static_cast<T *>(r.get()));
    }

...allows the removal of boost::detail::static_cast_tag and the
shared_ptr constructor that accepts it.

Regards,

--Clive.


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