1.检查类中是否有成员函数,不包括继承的成员函数
//例子1
template<typename T>
struct has_no_destroy
{
template <typename C> static char test(decltype(&C::no_destroy));
template <typename C> static int32_t test(...);
const static bool value = sizeof(test<T>(0)) == 1;
};
//例子2
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);
};
2.检查类中是否有成员函数, 包括继承的成员函数
template <typename Type>
class has_foo
{
class yes { char m;};
class no { yes m[2];};
struct BaseMixin
{
void foo(){}
};
struct Base : public Type, public BaseMixin {};
template <typename T, T t> class Helper{};
template <typename U>
static no deduce(U*, Helper<void (BaseMixin::*)(), &U::foo>* = 0);
static yes deduce(...);
public:
static const bool result = sizeof(yes) == sizeof(deduce((Base*)(0)));
};
3. https://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-member-functions 参考这篇文章,内容更丰富