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