tags:

views:

49

answers:

2

Possible Duplicate:
C++ class template of specific baseclass

class Base
{
...
};

class Derived1 : public Base
{
...
};

class Derived2 : public Base
{
...
};

class Unrelated
{
...
};

I want to have a class template ClassTemplate that accepts as parameter only classes Derived1 and Derived2 but not Unrelated, so I can do:

ClassTemplate<Derived1> object1;

ClassTemplate<Derived2> object2;

but I shouldn't do:

ClassTemplate<Unrelated> object3;

Is it possible at all?

A: 

Use boost static assert in combination with type traits

DeadMG
+4  A: 

Use boost::is_base_of from Boost.TypeTraits:

template<class T> class ClassTemplate {
    BOOST_STATIC_ASSERT((boost::is_base_of<Base, T>::value));
};
Georg Fritzsche