|
Boost : |
Subject: Re: [boost] [smart_ptr] enable_shared_from_this and multiple inheritance solution
From: Philippe Cayouette (pcayouette_at_[hidden])
Date: 2010-11-30 13:23:59
On 2010-11-29 22:56, Ansel Sermersheim wrote:
>> Did you tried your code on GCC? I just applied your patch and I think
>> I am encountering the same problem as my example: on GCC, the function
>> sp_enable_shared_from_this that is called is the one with the variable
>> arguments (...), since the compiler cannot be sure of the value of T
>> in the expression enable_shared_from_polymorphic< T> (it might be
>> Base1 or Base2). On VS2005, the compiler chooses Base1 (in the
>> original example) and thus, the pe->_internal_accept_owner function is
>> called on the unique enable_shared_from_polymorphic_base and
>> everything works (I guess, I didn't try).
>
> This is rather puzzling. I do develop on GCC, I tested with:
>
> bash$ g++ --version
> g++ (Debian 4.4.5-6) 4.4.5
> Copyright (C) 2010 Free Software Foundation, Inc.
Sorry for the confusion, your original patch did actually build on my
system, however, the compiler does not know which Base to take in
sp_enable_shared_from_this, so it decides to go with the (...) function
instead (please read again my explanation above for details, you might
also want to step into the code to see for yourself). So at runtime, the
internal weak_ptr never get initialized.
> However, your idea of having the _internal_accept_owner function take
> an enable_shared_from_polymorphic base makes perfect sense, and I have
> made that change. Hopefully that builds more happily for you.
This should solve the problem.
>>> If anyone is interested in the code, it can be found at:
>>>
>>> http://208.106.110.44/~ansel/boost/enable_shared_from_polymorphic.patch
>>>
>>> This patch is against the current SVN version of boost.
>
> The only cast that occurs in this code is a static_cast that downcasts
> from enable_shared_from_polymorphic<T> to T. I have read the relevant
> portions of the standard several times and I beleive this is valid,
> but I would definitely be curious if wiser heads agree with my reading.
To my understanding, you cannot downcast statically from a virtual base,
so I wonder how you can make it without dynamic cast, I will try your
latest patch and get back to you. The passage from
enable_shared_from_polymorphic<T> to T is no brainer, it is the passage
from enable_shared_from_polymorphic_base to
enable_shared_from_polymorphic<T> that I think is unknown at compile time...
Philippe
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk