How to ensure that a class template argument is derived from a specific Foo class?

Possible duplicate:
C ++ class template for a specific base layer

class Base
{
...
};

class Derived1 : public Base
{
...
};

class Derived2 : public Base
{
...
};

class Unrelated
{
...
};

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

ClassTemplate<Derived1> object1;

ClassTemplate<Derived2> object2;

but I should not do:

ClassTemplate<Unrelated> object3;

Is it possible at all?

+3
source share
2 answers

Use boost::is_base_offrom Boost.TypeTraits:

template<class T> class ClassTemplate {
    BOOST_STATIC_ASSERT((boost::is_base_of<Base, T>::value));
};
+5
source

Use boost static assert in combination with features

+1
source

Source: https://habr.com/ru/post/1750810/


All Articles