fork(1) download
  1. #include<iostream>
  2.  
  3. class WithMethod
  4. {
  5. public: int Method () const { return 1000; }
  6. };
  7.  
  8. class NoMethod {};
  9.  
  10. template<typename Class>
  11. class HasMember_Method
  12. {
  13. typedef char (&yes)[2];
  14.  
  15. template<unsigned int> struct Exists;
  16.  
  17. template<typename V>
  18. static yes CheckMember (Exists<sizeof(&V::Method)>*);
  19. template<typename>
  20. static char CheckMember (...);
  21.  
  22. public:
  23. static const bool value = (sizeof(CheckMember<Class>(0)) == sizeof(yes));
  24. };
  25.  
  26. template<typename T>
  27. void foo (const T* const p)
  28. {
  29. std::cout << "HasMember_Method<T>::value = " << HasMember_Method<T>::value << "\n";
  30. }
  31.  
  32. int main()
  33. {
  34. WithMethod* pW = new WithMethod;
  35. NoMethod* pN = new NoMethod;
  36.  
  37. foo(pW);
  38. foo(pN);
  39.  
  40. delete pW; delete pN;
  41. }
  42.  
  43.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
HasMember_Method<T>::value = 1
HasMember_Method<T>::value = 0