C
Christof Warlich
Hi,
is there a way in C++ to check whether a class is derived from
a specific class? Initially, I tried to solve this problem through
template specialization, e.g.:
#include <iostream>
class Base {
};
template<typename T> class A {
public:
void f() {std::cout << "T is not a Base\n";}
};
template<> class A<Base> {
public:
void f() {std::cout << "T is a Base\n";}
};
class Derived: public Base {
};
class Any {
};
int main() {
A<Any> a1;
a1.f();
A<Derived> a2;
a2.f();
A<Base> a3;
a3.f();
}
Output:
T is not a Base
T is not a Base
T is a Base
As to be expected, Derived will be identified as not being a Base. Is
there any way to specialize a template for a complete class hierarchy
instead of just specific types? Any other ideas to execute different
code depending on whether the type being passed to a template is derived
from a certain base or not?
Thanks for any help,
Christof
is there a way in C++ to check whether a class is derived from
a specific class? Initially, I tried to solve this problem through
template specialization, e.g.:
#include <iostream>
class Base {
};
template<typename T> class A {
public:
void f() {std::cout << "T is not a Base\n";}
};
template<> class A<Base> {
public:
void f() {std::cout << "T is a Base\n";}
};
class Derived: public Base {
};
class Any {
};
int main() {
A<Any> a1;
a1.f();
A<Derived> a2;
a2.f();
A<Base> a3;
a3.f();
}
Output:
T is not a Base
T is not a Base
T is a Base
As to be expected, Derived will be identified as not being a Base. Is
there any way to specialize a template for a complete class hierarchy
instead of just specific types? Any other ideas to execute different
code depending on whether the type being passed to a template is derived
from a certain base or not?
Thanks for any help,
Christof