Using SFINAE, i can detect wether a given class has a certain member function. But what if i want to test for inherited member functions?
The following does not work in VC8 and GCC4 (i.e. detects that A
has a member function foo()
, but not that B
inherits one):
#include <iostream>
template<typename T, typename Sig>
struct has_foo {
template <typename U, U> struct type_check;
template <typename V> static char (& chk(type_check<Sig, &V::foo>*))[1];
template <typename > static char (& chk(...))[2];
static bool const value = (sizeof(chk<T>(0)) == 1);
};
struct A {
void foo();
};
struct B : A {};
int main()
{
using namespace std;
cout << boolalpha << has_foo<A, void (A::*)()>::value << endl; // true
cout << boolalpha << has_foo<B, void (B::*)()>::value << endl; // false
}
So, is there a way to test for inherited member functions?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…