Boost logo

Boost :

From: John Maddock (John_Maddock_at_[hidden])
Date: 2002-01-11 07:15:48

>One thing I want which I don't think is in the proposal is the ability to
determine at compile-time whether or not a type is polymorphic.

I am building a runtime representation of the inheritance hierarchy for
conversion in Boost.Python. For polymorphic types, I need to generate
downcasts. As it stands, the interface is clumsy because the user has to
tell the library whether a given base class is polymorphic.

If a type X is not polymorphic, it is illegal to even write


where typeid(a)==typeid(X), unless Y is a base class of X.

Since it seems highly unlikely that we'll get the compile-time "tell me
whether this expression would be an error" operator, we ought to be
aggressive about pursuing things that help us avoid making errors in the
first place ;-)

Funny you should mention that, I came up with a reasonably protable
implementation for that the other day - I just haven't had a chance to try
it out. Heres my rather quickly knocked up code:

#include <boost/type_traits.hpp>
#include <boost/type_traits/type_traits_test.hpp>

template <class T>
struct is_polymorphic
   struct d1 : public T
      char padding[256];
   struct d2 : public T
      virtual ~d2();
      virtual void foo();
      char padding[256];
   BOOST_STATIC_CONSTANT(bool, value = (sizeof(d2) == sizeof(d1)));

int main(int argc, char* argv[])
   value_test(false, is_polymorphic<Base>::value);
   value_test(false, is_polymorphic<Derived>::value);
   value_test(false, is_polymorphic<NonDerived>::value);
   value_test(true, is_polymorphic<VB>::value);
   value_test(true, is_polymorphic<VD>::value);
   return 0;
unsigned int expected_failures = 0;

Works for Borland/gcc/VC6 and VC7.

- John Maddock

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