class HasFoo
{
public :
template <typename C, void (C::*) ()> class SFINAE {};
template <typename C> static bool test (SFINAE<C, &C::foo> *)
{
return true;
}
template <typename C> static bool test (...)
{
return false;
}
};
class B
{
public :
virtual void foo () {}
};
class D1 : public B
{
public :
void foo () {}
};
class D3 : public B {};
#include <stdio.h>
int main ()
{
bool ret=HasFoo::test<B>(0);
printf("%d\n",ret );
ret=HasFoo::test<D1>(0);
printf("%d\n",ret );
ret=HasFoo::test<D3>(0);
printf("%d\n",ret );
}
== === 输出 ======
jim@jim-destop:~$ a.out
1
1
0