Boost logo

Boost :

From: Greg Colvin (gcolvin_at_[hidden])
Date: 2001-05-19 12:58:11


In general I don't think this can be made to work without access to the
smart pointer internals.

I think the right thing to do is to implement boost::polymorphic_cast
for boost::shared_ptr. I (and Beman too, I think) would happily accept
such a patch.

----- Original Message -----
From: George A. Heintzelman <georgeh_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Saturday, May 19, 2001 11:13 AM
Subject: Re: [boost] Re: downcasting smart pointers

> > --- In boost_at_y..., "Rainer Deyke" <root_at_r...> wrote:
> > > The correct value if 'bp' at this point is '0'. Your function does
> > not
> > > mirror this behavior. The following fixes that problem:
> > >
> > > template<typename T, typename U>
> > > inline shared_ptr<T>& sp_dynamic_cast(const shared_ptr<U>& a)
> > > {
> > > if (dynamic_cast<T *>(a.get()) {
> > > return *(shared_ptr<T>*)&a;
> > > }
> > > return shared_ptr<T>(0);
> > > }
> >
> > Agreed about the returning 0. But isn't there a problem here with
> > returning a temporary? I changed ito to inline shared_ptr<T>&
> > sp_dynamic_cast(const shared_ptr<U>& a)
>
> I don't believe that this code is correct. You are assuming that the
> pointer to U included internally inside the shared_ptr a is also valid
> as a pointer to T, by forcing the compiler to assume that a can be
> treated as a shared_ptr to T as well as to U. But this may not be the
> case, particularly in the presence of multiple inheritance (where
> compilers often have to do pointer arithmetic to handle pointer casts).
> Because of this, I don't think you are going to be able to return a
> reference to a shared_ptr from this kind of function; you should go
> through the system's dynamic_cast mechanism and return a shared_ptr
> based on that result. This will I think require some mechanism in the
> shared_ptr for construction using another shared_ptr's reference count.
> How to do this safely needs some figuring out.
>
> I note that I would welcome having dynamic_cast available for smart
> pointers (and don't neglect std::auto_ptr when you do it), so I think
> the effort to do this is good, but I think that the attempt here will
> fail as it stands.
>
> George Heintzelman
> georgeh_at_[hidden]
>
>
>
>
> To unsubscribe, send email to: <mailto:boost-unsubscribe_at_[hidden]>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>


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