fork(3) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <utility>
  4. #include <type_traits>
  5.  
  6. using namespace std;
  7.  
  8. struct has
  9. {
  10. void operator()(int const&);
  11. };
  12.  
  13. struct hasNot1
  14. {
  15. void operator()(int);
  16. };
  17.  
  18. struct hasNot2
  19. {
  20. void operator()();
  21. };
  22.  
  23. struct hasNot3
  24. {
  25. void operator()(float);
  26. };
  27.  
  28. struct hasNot4
  29. {
  30. };
  31.  
  32. template<typename T, typename EDT>
  33. struct is_callable_oper
  34. {
  35. private:
  36. typedef char(&yes)[1];
  37. typedef char(&no)[2];
  38.  
  39. template <typename U, void (U::*)(EDT const &)> struct
  40. Check;
  41. template<typename>
  42. static yes test(...);
  43.  
  44. template <typename U>
  45. static no
  46. test(Check<U, &U::operator()>*);
  47.  
  48. public:
  49. static constexpr bool value = sizeof(test<T>(0))
  50. == sizeof(yes);
  51. };
  52.  
  53. int main() {
  54. cout << boolalpha << is_callable_oper<has, int&>::value << " "
  55. << is_callable_oper<has, int>::value << " "
  56. << is_callable_oper<hasNot1, int&>::value << " "
  57. << is_callable_oper<hasNot2, int&>::value << " "
  58. << is_callable_oper<hasNot3, int&>::value << " "
  59. << is_callable_oper<hasNot4, int&>::value << endl;
  60. return 0;
  61. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
true false true true true true