Boost logo

Boost :

Subject: Re: [boost] enable_shared_from_this2
From: Frank Mori Hess (frank.hess_at_[hidden])
Date: 2009-10-27 13:36:51


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Monday 26 October 2009, Berserker wrote:
> The "patched" code:
>
> void init_weak_once() const
> {
> // Reinitialization support
> if( weak_this_.expired() )
> {
> // shared_from_this in costructors support
> shared_this_.reset(static_cast
> <T*>(const_cast<enable_shared_from_this2>(this)),
> detail::esft2_deleter_wrapper());
> weak_this_ = shared_this_;
> }
> }
>
> I know that the static_cast/const_cast is "problematic" on VC6 (and
> maybe other compilers) but it could be solved using an offset from
> enable_shared_from_this2 * to T *.
> I've attached a testcase that shows the problems with the current
> enable_shared_from_this2 implementation (with the proposed patch it works).
> Any feedback about this?

IIRC, the original version of enable_shared_from_this that supported use in
constructors did a static_cast to the derived type. But it failed some of
the tests, so it was changed to a dynamic_cast. The version currently in svn
looks broken, like it was from a later version which relied on a
shared_from_this() free function like:

template<typename T>
shared_ptr<T> shared_from_this(T *);

which used shared_ptr aliasing to create and return a shared_ptr which pointed
at the same object as was passed in as an argument (so
enable_shared_from_this didn't have to do a cast or store a pointer to the
derived type). But I don't see any such free function in
enable_shared_from_this2.hpp.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkrnL7QACgkQ5vihyNWuA4XFAACfZR2MYD0G5yVKwykKTq+BGwU1
Zm8An0ChaK+AZX/Pp3CRVKzWJKRS2JjL
=Qxri
-----END PGP SIGNATURE-----


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