fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template<class T, class F1>
  5. class is_functor_enable {
  6. struct detector {};
  7. template<class Y>
  8. static auto search(Y &&)-> std::conditional_t<false, decltype(std::declval<F1>()(std::declval<Y>())), void>;
  9. static auto search(...) -> detector;
  10. public:
  11. using type = std::conditional_t<!std::is_same<decltype(search(std::declval<T>())), detector>::value, std::true_type, std::false_type>;
  12. };
  13.  
  14. #define GC_IS_HAS_METHOD(_arg_type, _arg_meth_name, ...) []() -> auto {\
  15. auto l = [](auto && lambda_arg) -> std::conditional<false, decltype(std::declval< decltype(lambda_arg) >(). _arg_meth_name ( __VA_ARGS__ )), void> {};\
  16. return is_functor_enable<_arg_type, decltype(l)>::type();\
  17. }()
  18.  
  19. template<class T>
  20. std::basic_ostream<T> &operator<<(std::basic_ostream<T> &s, const std::true_type &) {
  21. s << "true";
  22. return s;
  23. }
  24.  
  25. template<class T>
  26. std::basic_ostream<T> &operator<<(std::basic_ostream<T> &s, const std::false_type &) {
  27. s << "false";
  28. return s;
  29. }
  30.  
  31. int main() {
  32. // your code goes here
  33. std::cout
  34. << ' ' << GC_IS_HAS_METHOD(std::vector<int>, size, ) //is vector<int> has method size, which takes nothing
  35. << ' ' << GC_IS_HAS_METHOD(std::vector<int>, push_back, std::declval<int &>()) //is vector<int> has method push_back, which takes int &
  36. << ' ' << GC_IS_HAS_METHOD(std::vector<int>, insert, std::declval<std::vector<int>::iterator>(), std::declval<int &>()) //is vector<int> has method insert, which takes iterator and value
  37. << ' ' << GC_IS_HAS_METHOD(std::vector<int>, foo, 3,4,5,6,7) //is vector<int> has method foo, which takes 5 ints
  38. ;
  39. return 0;
  40. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
 true true true false