fork 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. static constexpr bool value = !std::is_same<decltype(search(std::declval<T>())), detector>::value;
  12. };
  13.  
  14. #define GC_IS_HAS_METHOD(_arg_type, _arg_meth_name, ...) ({\
  15. auto l = [](auto && lambda_arg) -> std::conditional<false, decltype(std::declval< decltype(lambda_arg) >(). _arg_meth_name ( __VA_ARGS__ )), void> {};\
  16. is_functor_enable<_arg_type, decltype(l)>::value;\
  17. })
  18.  
  19. int main()
  20. {
  21. static_assert(GC_IS_HAS_METHOD(std::vector<int>, foo, 3,4,5,6,7), "ты хуй");
  22. std::cout
  23. << ' ' << GC_IS_HAS_METHOD(std::vector<int>, size, ) //is vector<int> has method size, which takes nothing
  24. << ' ' << GC_IS_HAS_METHOD(std::vector<int>, push_back, std::declval<int &>()) //is vector<int> has method push_back, which takes int &
  25. << ' ' << 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
  26. << ' ' << GC_IS_HAS_METHOD(std::vector<int>, foo, 3,4,5,6,7) //is vector<int> has method foo, which takes 5 ints
  27. ;
  28. }
Compilation error #stdin compilation error #stdout 0s 15240KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:21:2: error: static assertion failed: ты хуй
  static_assert(GC_IS_HAS_METHOD(std::vector<int>, foo, 3,4,5,6,7), "ты хуй");
  ^~~~~~~~~~~~~
stdout
Standard output is empty