fork(1) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. #define declare_member_check(name, ret_type, /*args*/ ...) \
  5. namespace member_check{ \
  6. template <typename T> \
  7. struct has_##name \
  8. { \
  9.   template <typename U> static std::true_type test(decltype(&U::name)); \
  10.   template <typename U> static std::false_type test(...); \
  11.   static constexpr bool value = std::is_same<decltype(test<T>(nullptr)), std::true_type>::value; \
  12. };\
  13. }\
  14.  
  15. struct Test {
  16. const std::string &print(const std::string &str, void *pointer, int *second) {
  17. std::cout<<str<<std::endl;
  18. return str;
  19. }
  20. };
  21.  
  22. struct Test2 {
  23. void show_msg(){}
  24. };
  25.  
  26. declare_member_check(show_msg, void);
  27. declare_member_check(print, const std::string &, const std::string &, void *, int *);
  28.  
  29. int main()
  30. {
  31. std::cout << "Check show_msg" << std::endl;
  32. std::cout << "Test: " << (member_check::has_show_msg<Test>::value ? "yes" : "no") <<std::endl;
  33. std::cout << "Test2: " << (member_check::has_show_msg<Test2>::value ? "yes" : "no") <<std::endl;
  34.  
  35. std::cout << "Check print msg" << std::endl;
  36. std::cout << "Test: " << (member_check::has_print<Test>::value ? "yes" : "no") << std::endl;
  37. std::cout << "Test2: " << (member_check::has_print<Test2>::value ? "yes" : "no") << std::endl;
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
Check show_msg
Test: no
Test2: yes
Check print msg
Test: yes
Test2: no