Given the following code sequence:
#include <iostream>
using namespace std;
template <typename T>
class Base
{
public:
T* t;
void b() {}
};
class D1:
public Base<D1>
{
public:
int d1;
};
class D2:
public D1
{
public:
int d2;
};
template <typename T>
class Selector
{
public:
template <typename U>
void a(Base<U>& base)
{
cout << __LINE__ << endl;
base.b();
}
template <typename U>
void a(U& u)
{
cout << __LINE__ << endl;
}
};
int main()
{
D2 derivated;
Selector<D2> s;
s.a(derivated);
return 0;
}
I want to check whether, some class(D2) have base (Base) , inherited any of D2 parents.
I just can’t get Selector to hit the most specialized member function.
You can rig up your own trait to check whether a type has any
Base<T>as an ancestor. The following works for me:There’s usually no need to require any object instances for those sort of type checks; everything is just static. If you have an object, use
decltypeto get at its type, or add a type-deducing helper function.