fork download
  1. #include<iostream>
  2.  
  3. #define Contains(NAME) \
  4. template<typename T> \
  5. struct Contains_##NAME \
  6. { \
  7.   typedef char (&yes)[2]; \
  8. \
  9.   template<unsigned int> struct exists; \
  10.   template<typename V> static yes checkFunc (exists<sizeof(&V::NAME)>*); \
  11.   template<typename> static char checkFunc (...); \
  12. \
  13.   static const bool value = (sizeof(checkFunc<T>(0)) == sizeof(yes)); \
  14. }
  15.  
  16. #define ContainsType(NAME) \
  17. template<typename T, typename Function> \
  18. struct ContainsType_##NAME \
  19. { \
  20.   typedef char (&yes)[2]; \
  21. \
  22.   template<unsigned int> struct exists; \
  23.   template<typename V> static yes checkFunc (exists<sizeof(static_cast<Function>(&V::NAME))>*); \
  24.   template<typename> static char checkFunc (...); \
  25. \
  26.   static const bool value = (sizeof(checkFunc<T>(0)) == sizeof(yes)); \
  27. }
  28.  
  29. struct X {
  30. int begin () const;
  31. };
  32.  
  33. struct Y {
  34. int begin ();
  35. int begin () const;
  36. };
  37. struct Z {
  38. int begin ();
  39. };
  40.  
  41. Contains(begin);
  42. ContainsType(begin);
  43.  
  44. int main ()
  45. {
  46. std::cout << "Contains_begin<X>::value = " << Contains_begin<X>::value << "\n";
  47. std::cout << "Contains_begin<Y>::value = " << Contains_begin<Y>::value << "\n";
  48. std::cout << "Contains_begin<Z>::value = " << Contains_begin<Z>::value << "\n";
  49.  
  50. std::cout << "ContainsType_begin<X, int (X::*)()>::value = " << ContainsType_begin<X, int (X::*)() const>::value << "\n";
  51. std::cout << "ContainsType_begin<Y, int (Y::*)()>::value = " << ContainsType_begin<Y, int (Y::*)() const>::value << "\n";
  52. std::cout << "ContainsType_begin<Z, int (Z::*)()>::value = " << ContainsType_begin<Z, int (Z::*)() const>::value << "\n";
  53. }
  54.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
Contains_begin<X>::value = 1
Contains_begin<Y>::value = 0
Contains_begin<Z>::value = 1
ContainsType_begin<X, int (X::*)()>::value = 1
ContainsType_begin<Y, int (Y::*)()>::value = 1
ContainsType_begin<Z, int (Z::*)()>::value = 0