Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-04-05 15:24:07


From: "Gary Powell" <Gary.Powell_at_[hidden]>

> > This covers the (void*) casts, the only cast left is in
> >
> > template<typename Y>
> > auto_ptr(auto_ptr<Y>& rhs) throw()
> > : inherited((void *)((element_type *) rhs.release() ) )
> > {}
> >
> > Isn't the (element_type*) cast dangerous?
> <<
> Yes, and it isn't necessary for MSVC or gcc 2.95.2. (I'll cite bug for
now)
> So I've removed it in my copy.
> (will repost to the vault shortly.)

Wait a minute. If you remove the (element_type*) cast, you'll cast
rhs.release() (of type Y*) to void*. Then in ~auto_ptr you call delete
this->get(), where get() is:

      pointer get() const throw() { return (pointer)get_raw_ptr(); }

so you basically will delete an Y * reinterpret_cast'ed to a X*. Not good.
You need to use a static_cast<element*> in the templated constructor.

A possible alternative that avoids these problems is to make auto_ptr_base a
template.

> In that light, are you suggesting that auto_array_ptr not use
auto_ptr_ref?

No, I think that the _ptr and _array should be similar; if auto_ptr supports
pass/return by value, then so would auto_array_ptr. I won't use the feature,
but others obviously think it's worth it.

--
Peter Dimov
Multi Media Ltd.

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