Boost logo

Boost :

Subject: Re: [boost] Downcast taking account of virtual inheritance
From: Kim Barrett (kab.conundrums_at_[hidden])
Date: 2010-02-14 16:52:56


On Feb 14, 2010, at 2:55 PM, vicente.botet wrote:
> The implementation is based on whether the base type is polymorphic, not whether the base type is_virtual_base_of<B,D>.
> I have replaced
>
> template<class U>
> static T cast(U * u){
> typedef
> BOOST_DEDUCED_TYPENAME mpl::eval_if<
> BOOST_DEDUCED_TYPENAME mpl::and_<
> mpl::not_<is_base_and_derived<
> BOOST_DEDUCED_TYPENAME remove_pointer<T>::type,
> U
>>> ,
> mpl::not_<is_base_and_derived<
> U,
> BOOST_DEDUCED_TYPENAME remove_pointer<T>::type
>>>
>> ,
> mpl::identity<cross>,
> mpl::identity<linear>
>> ::type typex;
> return typex::cast(u);
> }
> by
> template<class U>
> static T cast(U * u){
> typedef
> BOOST_DEDUCED_TYPENAME mpl::eval_if<
> is_virtual_base_of<U, BOOST_DEDUCED_TYPENAME remove_pointer<T>::type>,
> mpl::identity<cross>,
> mpl::identity<linear>
>> ::type typex;
> return typex::cast(u);
> }
>
>
> And it work also. But now dynamic_cast is used only if U is virtual_base_of T.

I don't think this change is correct, as it will result in using static_cast in an actual cross-cast situation, i.e. when there is no base/derived relationship between U and T, but they have a common derived type of which the value being cast is an instance.

However, it seems to me that there is a bug in the existing smart_cast implementation, in that it may incorrectly attempt to use static_cast in the case where U is a virtual_base_of T.

I think the correct fix is to leave the existing derivation test in place, but augment it with the is_virtual_base_of test, i.e. mpl::or_ the existing test for cross-cast (T and U have no base/derived relationship) with the is_virtual_base_of test.


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