fork download
  1. #include <iostream>
  2.  
  3. int& func()
  4. {
  5. static int p;
  6. return p;
  7. }
  8.  
  9. int first_param() { return func() = 0 ; }
  10.  
  11. int next_param() { return ++func() ; }
  12.  
  13. template < std::size_t N > struct do_call_fn ;
  14.  
  15. template <> struct do_call_fn<0>
  16. { template < typename FN > static int call( FN fn) { return fn() ; } };
  17.  
  18. template <> struct do_call_fn<1>
  19. {
  20. template < typename FN > static int call( FN fn)
  21. { return fn( first_param() ) ; }
  22. };
  23.  
  24. template <> struct do_call_fn<2>
  25. {
  26. template < typename FN > static int call( FN fn)
  27. { auto a = first_param() ; return fn( a, next_param() ) ; }
  28. };
  29.  
  30. template <> struct do_call_fn<3>
  31. {
  32. template < typename FN > static int call( FN fn)
  33. {
  34. auto a = first_param() ;
  35. auto b = next_param() ;
  36. return fn( a, b, next_param() ) ;
  37. }
  38. };
  39.  
  40. template <> struct do_call_fn<4>
  41. {
  42. template < typename FN > static int call( FN fn)
  43. {
  44. auto a = first_param() ;
  45. auto b = next_param() ;
  46. auto c = next_param() ;
  47. return fn( a, b, c, next_param() ) ;
  48. }
  49. };
  50. template < typename... ARGS > int call_fn( int (&fn)( ARGS... ) )
  51. { return do_call_fn< sizeof...(ARGS) >::call(fn) ; }
  52.  
  53. int f() { std::cout << "f()\n" ; return 0 ; }
  54. int g(int) { std::cout << "g(int)\n" ; return 1 ; }
  55. int h(int,int) { std::cout << "h(int,int)\n" ; return 2 ; }
  56. int i(int,int,int) { std::cout << "i(int,int,int)\n" ; return 3 ; }
  57. int j(int,int,int,int) { std::cout << "j(int,int,int,int)\n" ; return 3 ; }
  58.  
  59. int main()
  60. {
  61. call_fn(f) ;
  62. call_fn(g) ;
  63. call_fn(h) ;
  64. call_fn(i) ;
  65. call_fn(j) ;
  66. }
  67.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
f()
g(int)
h(int,int)
i(int,int,int)
j(int,int,int,int)