Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-01-14 15:47:05


----- Original Message -----
From: "Luigi Ballabio" <ballabio_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Monday, January 14, 2002 3:21 PM
Subject: Re: [boost] shared_ptr: unsafe, or am I crazy?

> > > At 08:56 AM 1/14/02 -0500, you wrote:
> >> >struct Base { Base(); ~Base(); };
> >> >struct Derived : Base { Derived(); ~Derived(); };
> >> >
> >> >void unsafe()
> >> >{
> >> > shared_ptr<Derived> d(new Derived);
> >> > shared_ptr<Base> b(d);
> >> > d.reset();
> >> > b.reset(); // equivalent to delete (Base*)(new Derived)!!
> >> >};
> >> >
> >> >Isn't shared_ptr just screaming for the application of John Maddock's
new
> >> >is_polymorphic<> trait?
> >>
> >> Maybe I'm just about to embarass myself, but isn't Base just screaming
for
> >> having a virtual destructor instead, if you want to use it this way?
> >
> >The point is that non-polymorphic public bases are an important and
useful
> >construct, and safe as long as you don't explicitly delete a derived
through
> >the base pointer.
>
> I wholeheartedly agree, and my point was that they should be used
> exactly as such---as non-polymorphic. Maybe I'm just being dense (no
> irony. Maybe I'm just being dense. Really.) but what would be the
> reason for upcasting a derived class to its base,

You should have read past my first sentence:

> > However, since shared_ptr deletes "implicitly",
> > allowing implicit conversion of shared_ptr<Derived>
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > to shared_ptr<Base> can be problematic.

There is no upcast, only an implicit conversion. When a deadly usage
silently compiles, it's a problem that should be corrected, especially if we
have the tools to do so.

-Dave


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