|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-02-12 08:34:07
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.)
It does not matter what the static type of the shared_ptr is, i.e. what the
T is in shared_ptr<T>; the actual pointer type needs to be "foo*".
shared_ptr<void const volatile> p( new foo );
is enough for shared_from_this() to work (provided that you find a way to
call it, of course.)
The reason for this restriction is that enable_shared_from_this<foo>
contains a weak_ptr<foo>, and in order to initialize it from a shared_ptr
that stores a pointer to const foo, the implementation would need to
const_cast the constness away twice, once in order to access the weak_ptr
member, a second time to initialize a weak_ptr<foo> from a shared_ptr<foo
const>.
> BTW: I believe that a conversion from auto_ptr<T> to shared_ptr<T
> const> doesn't compile on VC6, but I can't test that ATM(I'm not at
> work). If any kind soul wants to, they could add such a conversion to
> the regression-tests. Same goes for volatile probably.
I see multiple auto_ptr<T> to shared_ptr<T const> conversions in
shared_ptr_test.cpp, have you looked at it?
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk