fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<typename Ret, typename Param = void, typename ...Args>
  5. class Callback
  6. {
  7. public:
  8. template<typename T = Param>
  9. using Ptr = Ret (*)(Args..., T *param);
  10. Ptr<> m_func;
  11. Param *m_param;
  12. template<typename T = Param>
  13. void setFunc(Ptr<T> func) { m_func = (Ptr<>)func; }
  14. };
  15.  
  16. double f(int x, int *y)
  17. {
  18. return x+*y;
  19. }
  20.  
  21. int main() {
  22. int z;
  23. double d;
  24. Callback<double, void, int> c1;
  25. c1.setFunc(f);
  26. c1.setFunc<int>([](int x, int *y)->double
  27. {
  28. return x*(*y);
  29. });
  30.  
  31. Callback<double> c2{[](void *y)->double
  32. {
  33. return *(double *)y;
  34. }, &d};
  35. return 0;
  36. }
Success #stdin #stdout 0s 4328KB
stdin
Standard input is empty
stdout
Standard output is empty