fork(2) download
  1. // LIBRARY
  2. #define HasMember(NAME) \
  3.   template<class Class, typename Type = void> \
  4.   struct HasMember_##NAME \
  5.   { \
  6.   typedef char (&yes)[2]; \
  7.   template<unsigned long> struct exists; \
  8.   template<typename V> static yes Check (exists<sizeof(static_cast<Type>(&V::NAME))>*); \
  9.   template<typename> static char Check (...); \
  10.   static const bool value = (sizeof(Check<Class>(0)) == sizeof(yes)); \
  11.   }; \
  12.   template<class Class> \
  13.   struct HasMember_##NAME<Class, void> \
  14.   { \
  15.   typedef char (&yes)[2]; \
  16.   template<unsigned long> struct exists; \
  17.   template<typename V> static yes Check (exists<sizeof(&V::NAME)>*); \
  18.   template<typename> static char Check (...); \
  19.   static const bool value = (sizeof(Check<Class>(0)) == sizeof(yes)); \
  20.   }
  21.  
  22. // INSTANTIATE
  23. HasMember(Foo);
  24.  
  25. // USAGE
  26. struct A {
  27. int Foo(float);
  28. };
  29.  
  30. struct B : public A {
  31. };
  32.  
  33. struct C {
  34. unsigned int Foo(double);
  35. };
  36.  
  37. struct D {
  38. static int Foo(float);
  39. };
  40.  
  41. static_assert(HasMember_Foo<A>::value, "A should have foo.");
  42. static_assert(HasMember_Foo<B>::value, "B should inherit foo from A.");
  43.  
  44. static_assert(!HasMember_Foo<C, int (C::*)(float)>::value, "C should not have foo.");
  45. static_assert(!HasMember_Foo<D, int (D::*)(float)>::value, "Ds static foo should be false.");
  46.  
  47. int main () {}
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Standard output is empty