Boost logo

Boost :

From: Daniel Frey (d.frey_at_[hidden])
Date: 2008-04-09 08:07:02


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( // may throw

or is the current code some kind of optimization?

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?

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

Also, it seems that enable_shared_from_this is getting some new
features. Previously, enable_shared_from_this added a single weak_ptr to
the derived class. Now it adds a vtable, a shared_ptr, a weak_ptr and a
bool variable. Am I the only one who is concerned about this?

Before the change, I even wondered if the weak_ptr could be replaced by
a detail::weak_count, which would reduce the overhead from two plain
pointers to only one plain pointer, since shared_from_this always has

Regards, Daniel

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