Boost logo

Boost :

From: SRobb_at_[hidden]
Date: 2004-07-07 04:45:37

  I wrote that very thing a year or so ago for the purpose of testing if a
base pointer type can be statically downcast to a derived type. I don't
claim to have tested all permutations of its usage, but it certainly worked
on the cases I was testing it with.

  However, it does have the unfortunate side-effect of generating an
'ambiguous base' warning on the later GCCs every time it is used with a
class which inherits another non-virtually. (though the traits class still
gives the correct answer) If anyone can find a way of preventing this
warning, or even an alternate implementation, I'd be glad to hear it.

  Here is my basic implementation:

#include <boost/config.hpp>
#include <boost/type_traits/is_convertible.hpp>

namespace is_virtual_base_and_derived_detail
        #ifdef _MSC_VER
                #pragma warning(push)
                #pragma warning(disable : 4250)

        template <typename BaseImpl, typename DerivedImpl>
        struct tester : public DerivedImpl, virtual public BaseImpl
                tester(const tester&);

        #ifdef _MSC_VER
                #pragma warning(pop)

        template <typename Base, typename Derived, bool IsBaseAndDerived>
        struct impl;

        template <typename Base, typename Derived>
        struct impl<Base, Derived, true>
                BOOST_STATIC_CONSTANT(bool, value = sizeof(tester<Base,
Derived>) == sizeof(Derived));

        template <typename Base, typename Derived>
        struct impl<Base, Derived, false>
                BOOST_STATIC_CONSTANT(bool, value = false);

template <typename Base, typename Derived>
struct is_virtual_base_and_derived :
is_virtual_base_and_derived_detail::impl<Base, Derived,
boost::is_convertible<Derived*, Base*>::value>

template <typename T>
struct is_virtual_base_and_derived<T, T>
        BOOST_STATIC_CONSTANT(bool, value = false);

-----Original Message-----
From: boost-bounces_at_[hidden] [mailto:boost-bounces_at_[hidden]]
On Behalf Of Robert Ramey
Sent: Tuesday, July 06, 2004 6:01 PM
To: boost_at_[hidden]
Subject: [boost] Does anyone have an is_virtual_base_and_derived ?

Type traits includes:


Does anyone have a version that might be called

is_virtual_base_and_derived<B, D> which would return true iff D is derived
virtually from B ?

Robert Ramey

Unsubscribe & other changes:

Boost list run by bdawes at, gregod at, cpdaniel at, john at