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. template<class R, class Arg1, class Arg2>
  35. function<void(int, value[])>
  36. thunk( R(*f)(Arg1, Arg2) )
  37. {
  38. return [f](int, value v[]) -> R
  39. {
  40. return f(v[0], v[1]);
  41. };
  42. }
  43.  
  44.  
  45. void f1(int p1) { cout << "f1\t" << p1 << "\n"; }
  46. void f2(int p1, int p2) { cout << "f2\t" << p1 << "\t" << p2 << "\n"; }
  47.  
  48. int main() {
  49. int a1[] = {1};
  50. int a2[] = {3, 4};
  51.  
  52. // case 1
  53. auto ufunc = thunk( f2 );
  54. ufunc(2,a2);
  55.  
  56.  
  57. // case 2
  58. auto ff2 = [](int p1, int p2) { cout << "ff2\t" << p1 << "\t" << p2 << "\n"; };
  59.  
  60. auto ufunc2 = thunk( ff2 );
  61. ufunc2(2,a2);
  62.  
  63. return 0;
  64. }
Compilation error #stdin compilation error #stdout 0s 3428KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:60:27: error: no matching function for call to ‘thunk(main()::__lambda3&)’
  auto ufunc2 = thunk( ff2 );
                           ^
prog.cpp:60:27: note: candidates are:
prog.cpp:10:1: note: template<class Signature> std::function<void(int, int*)> thunk(std::function<Signature>)
 thunk( function<Signature> f );
 ^
prog.cpp:10:1: note:   template argument deduction/substitution failed:
prog.cpp:60:27: note:   ‘main()::__lambda3’ is not derived from ‘std::function<Signature>’
  auto ufunc2 = thunk( ff2 );
                           ^
prog.cpp:15:1: note: template<class R, class Arg1> std::function<void(int, int*)> thunk(std::function<R(Arg1)>)
 thunk( function<R(Arg1)> f )
 ^
prog.cpp:15:1: note:   template argument deduction/substitution failed:
prog.cpp:60:27: note:   ‘main()::__lambda3’ is not derived from ‘std::function<R(Arg1)>’
  auto ufunc2 = thunk( ff2 );
                           ^
prog.cpp:26:1: note: template<class R, class Arg1, class Arg2> std::function<void(int, int*)> thunk(std::function<R(Arg1, Arg2)>)
 thunk( function<R(Arg1, Arg2)> f )
 ^
prog.cpp:26:1: note:   template argument deduction/substitution failed:
prog.cpp:60:27: note:   ‘main()::__lambda3’ is not derived from ‘std::function<R(Arg1, Arg2)>’
  auto ufunc2 = thunk( ff2 );
                           ^
prog.cpp:36:1: note: template<class R, class Arg1, class Arg2> std::function<void(int, int*)> thunk(R (*)(Arg1, Arg2))
 thunk( R(*f)(Arg1, Arg2) )
 ^
prog.cpp:36:1: note:   template argument deduction/substitution failed:
prog.cpp:60:27: note:   mismatched types ‘R (*)(Arg1, Arg2)’ and ‘main()::__lambda3’
  auto ufunc2 = thunk( ff2 );
                           ^
prog.cpp:49:6: warning: unused variable ‘a1’ [-Wunused-variable]
  int a1[] = {1};
      ^
stdout
Standard output is empty