Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2008-04-09 10:07:39


Daniel Frey wrote:
> Hi,
>
> I was asked to present smart_ptr to our department, but while trying
> to understand the details of the implementation, I found some things
> that I don't understand:
>
> 1) in shared_ptr.hpp, the ctor which takes a weak_ptr doesn't copy px
> from r.px in the initializer list like all other ctors, instead the
> copy
> is done the the ctor body. There is even a comment which suggests that
> there is a problem with doing it before pn, it would not be "safe".
> What exactly does this mean? px is just a plain pointer, so AFAICS it
> won't
> hurt to simply say:
>
> template<class Y>
> explicit shared_ptr(weak_ptr<Y> const & r)
> : px(r.px), pn(r.pn) // may throw
> {
> }

No, copying a deleted pointer is undefined behavior.

> 2) in detail/shared_count.hpp, operator= for shared_count has an
> optimization for tmp==pi_. At least I think that it's just an
> optimization and it would work without the check. OTOH, operator= for
> weak_count does not check for tmp==pi_. Why? Is there any deeper
> reason that these two code fragements differ?

It's a performance optimization. There's no deep reason for the difference.

> 3) I asked about this point before, but got no real answer: There is
> no throw specification in shared_from_this()'s documentation. AFAICS,
> shared_from_this() (if used correctly) can not throw. But will this be
> guaranteed?

I'm not sure. It's not guaranteed by TR1 or the draft standard, and I
haven't considered the issue in detail to be confident that this is a
defect.


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