Boost logo

Boost :

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


"Peter Dimov" <pdimov_at_[hidden]> wrote in message
news:003701c1e85a$1847bda0$1d00a8c0_at_pdimov2...
> From: "Fernando Cacciola" <fcacciola_at_[hidden]>
> > I wonder if this could be confusing: shared_ptr uses a detached or
> intrusive
> > count depending on how it is constructed.... I find more intuitive to
> expect
> > that shared_ptr uses a detached or intrusive count depending on the
> pointee
> > type, regardless of how you create it.
>
> Let's assume that shared_ptr autodetects. Consider my original example:
>
> // some member function
>
> shared_ptr<this_type> f()
> {
> return shared_ptr<this_type>(this);
> }
>
> Is this correct? Yes, if this_type is derived from counted_base, undefined
> behavior otherwise.
>
> Now consider the explicit version:
>
> // some member function
>
> shared_ptr<this_type> f()
> {
> return intrusive_ptr<this_type>(this);
> }
>
> Now the compiler can verify the correctness.
>
> It's always better when the correctness of the code can be determined
> without context.
>
Yes, this is the example you mentioned earlier. I think this is the only
case when using the embedded count under hoods is wrong. (when returning
'this')
Still, one single case is enough... I think that this problem in itself
worth a separate intrusive_ptr<>.

--
Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com

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