fork(11) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template<typename T>
  5. struct has_size_method
  6. {
  7. private:
  8. typedef std::true_type yes;
  9. typedef std::false_type no;
  10.  
  11. template<typename U, int (U::*f)() const> struct SFINAE{};
  12. template<typename U, std::size_t (U::*f)() const> struct SFINAE<U,f>{};
  13.  
  14.  
  15. template<class C> static yes test(SFINAE<C,&C::size>*);
  16. template<class C> static no test(...);
  17.  
  18. public:
  19. static constexpr bool value = std::is_same<yes,decltype(test<T>(nullptr))>::value;
  20. };
  21.  
  22. struct x
  23. {
  24. int size() const { return 42; }
  25. };
  26.  
  27. struct y : x {};
  28.  
  29. int main()
  30. {
  31. std::cout << has_size_method<x>::value << std::endl;
  32. std::cout << has_size_method<y>::value << std::endl;
  33. }
  34.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
1
0