Guest User

Untitled

a guest
Dec 14th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. template<typename T>
  5. struct Method {};
  6.  
  7. template<>
  8. struct Method<int> {
  9. int operator()() { return 1; }
  10. };
  11.  
  12. template<class T>
  13. struct is_brackets_op_defined {
  14. static void Check(...);
  15.  
  16. template<typename C>
  17. static decltype(Method<C>::operator()) Check(const C&);
  18.  
  19. using type = decltype(Check(
  20. std::declval< Method<T> >()
  21. ));
  22.  
  23. constexpr static bool value =
  24. !std::is_same<void, type>();
  25. };
  26.  
  27. int main() {
  28. std::cout << is_brackets_op_defined<float>::value << std::endl;
  29. }
  30.  
  31. std::cout << is_brackets_op_defined<float>::value // false
  32. std::cout << is_brackets_op_defined<int >::value // false
  33.  
  34. #include <iostream>
  35. #include <functional>
  36.  
  37. template<typename T>
  38. struct Method;
  39.  
  40. template<>
  41. struct Method<int> {
  42. int operator()() { return 1; }
  43. };
  44.  
  45. template<class T>
  46. struct is_brackets_op_defined {
  47. static std::false_type Check(...);
  48.  
  49. template<typename C, class = decltype(&C::operator())>
  50. static std::true_type Check(const C*);
  51.  
  52. using type = decltype(Check(static_cast<Method<T>*>(nullptr)));
  53.  
  54. constexpr static bool value = type();
  55. };
  56.  
  57. int main() {
  58. std::cout << std::boolalpha << is_brackets_op_defined<float>::value << std::endl;
  59. std::cout << std::boolalpha << is_brackets_op_defined<int>::value << std::endl;
  60. }
  61.  
  62. template<typename C>
  63. static decltype(std::declval<C>().operator()()) Check(const C&);
  64.  
  65. #include <iostream>
  66. #include <type_traits>
  67.  
  68. template<class T> struct
  69. is_brackets_op_defined
  70. {
  71. private: template<typename C> static
  72. int Check(...);
  73.  
  74. private: template<typename C> static
  75. void Check(decltype(&C::operator ()));
  76.  
  77. public: constexpr static bool value
  78. {
  79. not std::is_same_v<int, decltype(Check<T>(nullptr))>
  80. };
  81. };
  82.  
  83. struct ClassWith
  84. {
  85. void operator ()() {}
  86. };
  87.  
  88. struct ClassWithout
  89. {
  90. };
  91.  
  92. int main()
  93. {
  94. std::cout << is_brackets_op_defined<float>::value << std::endl;
  95. std::cout << is_brackets_op_defined<ClassWith>::value << std::endl;
  96. std::cout << is_brackets_op_defined<ClassWithout>::value << std::endl;
  97. return 0;
  98. }
Add Comment
Please, Sign In to add comment