使用SFINAE,我可以检测给定类是否具有某些成员函数。但是,如果我想测试继承的成员函数怎么办?
以下内容在VC8和GCC4中不起作用(即检测到A具有成员函数foo(),但不能B继承成员函数):
#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
}
那么,有没有办法测试继承的成员函数?
蛊毒传说
相关分类