fork(1) download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. using namespace std;
  5.  
  6. typedef int value;
  7.  
  8. template<class Signature>
  9. function<void(int, value[])>
  10. thunk( function<Signature> f );
  11.  
  12.  
  13. template<class R, class Arg1>
  14. function<void(int, value[])>
  15. thunk( function<R(Arg1)> f )
  16. {
  17. return [f](int, value v[]) -> R
  18. {
  19. return f(v[0]);
  20. };
  21. }
  22.  
  23.  
  24. template<class R, class Arg1, class Arg2>
  25. function<void(int, value[])>
  26. thunk( function<R(Arg1, Arg2)> f )
  27. {
  28. return [f](int, value v[]) -> R
  29. {
  30. return f(v[0], v[1]);
  31. };
  32. }
  33.  
  34.  
  35. void f1(int p1) { cout << "f1\t" << p1 << "\n"; }
  36. void f2(int p1, int p2) { cout << "f2\t" << p1 << "\t" << p2 << "\n"; }
  37.  
  38. int main() {
  39. int a1[] = {1};
  40. int a2[] = {3, 4};
  41. thunk( std::function<void(int)>(f1) )(1, a1);
  42. thunk( std::function<void(int, int)>(f2) )(2, a2);
  43. return 0;
  44. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
f1	1
f2	3	4