fork(1) download
  1. #include <iostream>
  2.  
  3. template <bool, typename=void> struct enable_if {};
  4. template <typename T> struct enable_if<true, T> { typedef T type; };
  5.  
  6. template <typename T> T declval();
  7.  
  8. template<typename T>
  9. class test
  10. {
  11. typedef char yes[1];
  12. typedef char no[2];
  13.  
  14. template <typename U>
  15. static yes& check2(U T::*);
  16.  
  17. template <typename U>
  18. static typename enable_if<sizeof(check2(&U::operator()))==sizeof(yes), yes>::type& check(U);
  19.  
  20. static no& check(...);
  21.  
  22. public:
  23.  
  24. static const bool is_functor = sizeof(check(declval<T>())) == 1;
  25. };
  26.  
  27. struct a{};
  28. struct b
  29. {
  30. const volatile void operator() (a,a,a) const volatile {}
  31. };
  32.  
  33. int main()
  34. {
  35. std::cout << test<a>::is_functor << '\n';
  36. std::cout << test<b>::is_functor << '\n';
  37. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
0
1