Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test whether class B is derived from class A?

More specifically, let's say I have a class template with parameters A and B, and I would like to have a compiler error (when the template is being instantiated) if B is not derived from A.

template<class A, class B>
class Foo
{
    // static_assert(B is derived from A)
};
like image 202
Timo Avatar asked Dec 26 '10 00:12

Timo


1 Answers

This has been asked so very many times before, but it's so simple I'll post the solution again:

~Foo()
{
    A* p = (B*)0; // B is required to be a subtype of A
}
like image 193
Ben Voigt Avatar answered Sep 23 '22 05:09

Ben Voigt