Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2002-04-24 11:35:19

"Peter Dimov" <pdimov_at_[hidden]> wrote in message
> I've changed the shared_ptr(T *) constructor to a member template,
> template<class Y> shared_ptr(Y *).
I've just looked at it.

Why is the cast from Y* to T* implicit?
AFAIK this could go wrong when complex polymorphism is involved:

shared_ptr<Base> pb( new Derived ) ;

My own smart ptr has both template and non template ctors.
The template ctor uses boost::polymorphic_cast.

(And similarly, assignment and reset have also both versions)

> This allows code like shared_ptr<void> pv(new int) or shared_ptr<X> px(new
> Y) (where X doesn't have a virtual destructor) to do the right thing. The
> former equivalent was the more verbose shared_ptr<X> px =
> Y).
> This breaks some existing code since '0' is no longer an acceptable
> argument; for instance, px.reset(0) needs to be rewritten as px.reset().
I think this could be resolved by having both template and non-template

> I'm still undecided about the case where Y is derived from counted_base.
> current behavior doesn't detect this and allocates an external reference
> count; having two separate reference counts for the same object is almost
> always a mistake.

> On the other hand, making the constructor autodetect is a semantic
> (a single function with multiple behaviors) which is typically to be
> (it breaks the use_count() == 1 postcondition in some cases.)
Isn't it broke already when using the ctor from intrusive_ptr<>?

FWIW, I have my own intrusive smart pointer, and boost::shared_ptr. I've
never needed to convert between them, so I wonder why should they

Fernando Cacciola
Sierra s.r.l.

Boost list run by bdawes at, gregod at, cpdaniel at, john at