fork download
  1. #include <type_traits>
  2. #include <utility>
  3.  
  4. template<typename T>
  5. class has_foo
  6. {
  7. private:
  8. template<typename U>
  9. static auto check( U v ) -> decltype( v.foo(), std::true_type() );
  10. static auto check( ... ) -> decltype( std::false_type() );
  11.  
  12. public:
  13. typedef decltype( check( std::declval<T>() ) ) type;
  14. static bool const value = type::value;
  15. };
  16.  
  17.  
  18. class klass
  19. {
  20. public:
  21. void foo() const {}
  22. };
  23.  
  24.  
  25. #include <iostream>
  26.  
  27. int main()
  28. {
  29. std::cout
  30. << has_foo<int>::value << std::endl
  31. << has_foo<klass>::value << std::endl;
  32.  
  33. }
Success #stdin #stdout 0s 2928KB
stdin
Standard input is empty
stdout
0
1