|
Boost : |
From: Joe Gottman (jgottman_at_[hidden])
Date: 2004-02-12 20:35:37
"Peter Dimov" <pdimov_at_[hidden]> wrote in message
news:00fc01c3f16c$e59d0220$1d00a8c0_at_pdimov2...
> Ulrich Eckhardt wrote:
> > Greetings!
> > Firstly, this is not a regression, the behaviour was present in 1.30,
> > too.
> > I have attached a simple program that should demonstrate the
> > behaviour, but the core is these lines, with foo being derived from
> > enable_shared_from_this:
> >
> > // 1
> > std::auto_ptr<foo> g(new foo);
> > boost::shared_ptr<foo> f(g);
> > // 2
> > std::auto_ptr<foo> g(new foo);
> > boost::shared_ptr<foo const> f(g);
> > // 3
> > std::auto_ptr<foo const> g(new foo);
> > boost::shared_ptr<foo const> f(g);
> >
> > In all three cases, I'd expect to be able to call shared_from_this().
> > However, only the first two cases really work. I think it boils down
> > to the function detail::sp_enable_shared_from_this() in the ctor of
> > shared_ptr<> that takes an auto_ptr<>.
>
> This is, well, "by design". enable_shared_from_this requires that at least
> one shared_ptr "owns" the plain address of the object (without any cv
> qualifiers.)
>
Why not make the _internal_weak_this member of enable_shared_from_this
mutable? As I understand it, an enable_shared_from_this<X> basically
undergoes two-phase construction: phase one at actual construction time, and
phase two when it is put into a shared_ptr. As an extra safety measure, you
could make _internal_weak_this a private member and make all shared_ptr<Y>'s
friends of enable_shared_from_this (for compilers that allow template
friends).
Joe Gottman
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk