How to check that a template class is obtained from this class at compile time?

I wonder if there is any elegant way (like this ) to check that the template argument is derived from this class? Generally:

template<class A, class B> class MyClass { // shold give the compilation error if B is not derived from A // but should work if B inherits from A as private } 

The solution presented in another question only works when B inherits from A as public:

 class B: public A 

however, I would prefer not to have such a restriction:

 class A{}; class B : public A{}; class C : private A{}; class D; MyClass<A,B> // works now MyClass<A,C> // should be OK MyClass<A,D> // only here I need a compile error 

Thanks in advance!

+4
source share
2 answers

Private inheritance from something is a part of implementation.

During refactoring and code analysis, I would be much happier if such detection were impossible for functionality outside ...

+1
source

You can try something like I said here: C ++: specifying the base class for the template parameter in a static statement (either C ++ 0x or BOOST_STATIC_ASSERT)

 template<class A, class B> class MyClass { static_assert( boost::is_base_of<A,B>::value ); } 
+7
source

All Articles