fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. template<typename... Ts>
  5. struct Executor { };
  6.  
  7. template<typename ReturnType, typename... Params>
  8. struct Executor<ReturnType (*)(Params...)> {
  9. private:
  10. // using M = ReturnType (*)(Params...);
  11. typedef ReturnType (*M)(Params...);
  12. public:
  13. bool operator()(M method, Params... params) {
  14. ReturnType r = method(params...);
  15.  
  16. return (r ? true : false);
  17. }
  18.  
  19. template<typename... InvalidParams>
  20. bool operator()(M method, InvalidParams... ts) {
  21. return false;
  22. }
  23. };
  24.  
  25. #define ExecuteMethod(M, ...) Executor<decltype(&M)>{}(M, ##__VA_ARGS__)
  26.  
  27.  
  28.  
  29. bool func(int i, int j, std::string& k) {
  30. k = "ho ho";
  31. return true;
  32. }
  33.  
  34. int main() {
  35. int i = 3;
  36. int j = 4;
  37. std::string s = "yo";
  38.  
  39. std::cout << s << std::endl;
  40.  
  41. bool a = ExecuteMethod(func, i, j, s);
  42. bool b = ExecuteMethod(func, 3, 4, "yo");
  43. bool c = ExecuteMethod(func, "yo", 3, 4);
  44.  
  45. std::cout << std::boolalpha << a << std::endl;
  46. std::cout << std::boolalpha << b << std::endl;
  47. std::cout << std::boolalpha << c << std::endl;
  48. std::cout << s << std::endl;
  49. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
yo
true
false
false
ho ho