fork download
  1. #include <iostream>
  2.  
  3. int function(int arg) { return arg; }
  4. void function2() { std::cout << "void" << std::endl; }
  5.  
  6. template <typename T> class example;
  7. template <typename T_ret, typename... T_arg>
  8. class example <T_ret (*) (T_arg...)> {
  9. public:
  10. using type_f = T_ret (*) (T_arg...);
  11. };
  12.  
  13. template <typename T>
  14. class caller {};
  15.  
  16. // for non-void:
  17. template <typename T_ret, typename... T_args>
  18. class caller<T_ret(*)(T_args...)> {
  19. T_ret (*m_p) (T_args... args);
  20. public:
  21. T_ret call (T_args... args) {
  22. return m_p(args...);
  23. }
  24. caller (T_ret (*p)(T_args...args)) : m_p(p) {}
  25. };
  26.  
  27. // for void:
  28. template <typename... T_args>
  29. class caller<void(T_args...)> {
  30. void (*m_p) (T_args... args);
  31. public:
  32. void call (T_args... args) {
  33. return m_p(args...);
  34. }
  35. caller (void (*p)(T_args...args)) : m_p(p) {}
  36. };
  37. typedef int (*function_int_t) (int arg);
  38. typedef void (*function_void_t) ();
  39. typedef example <function_int_t> example_int_t;
  40. typedef example <function_void_t> example_void_t;
  41.  
  42. int main()
  43. {
  44. caller<function_int_t> c1(&function);
  45. caller<function_void_t> c2(&function2);
  46. caller<example_int_t::type_f> c3(&function);
  47. caller<example_void_t::type_f> c4(&function2);
  48. std::cout << c1.call(1) << std::endl;
  49. c2.call();
  50. std::cout << c3.call(2) << std::endl;
  51. c4.call();
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
1
void
2
void