fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef int (*ptr1)(int, int);
  5. int add1(int a, int b){return a+b;}
  6.  
  7. typedef double (*ptr2)(double, double, double);
  8. double add2(double a, double b, double c){return a+b+c;}
  9.  
  10. template <typename TResult>
  11. class executor{
  12. class executable;
  13.  
  14. template<typename T1, typename T2, typename Func>
  15. class _e2;
  16. public:
  17. template <typename T1, typename T2, typename Func>
  18. executor(T1 arg1, T2 arg2, Func f) : e(new _e2<T1,T2,Func>(arg1, arg2, f)){}
  19.  
  20. template <typename T1, typename T2, typename T3, typename Func>
  21. executor(T1 arg1, T2 arg2, T3 arg3, Func f) : e(new _e3<T1,T2,T3,Func>(arg1, arg2, arg3, f)){}
  22.  
  23.  
  24. TResult execute() {return e->execute();}
  25. private:
  26. executable *e;
  27. private:
  28. class executable{
  29. public:
  30. virtual TResult execute() = 0;
  31. };
  32.  
  33. template<typename T1, typename T2, typename Func>
  34. class _e2 : public executable{
  35. public:
  36. _e2(T1 arg1, T2 arg2, Func f) : _arg1(arg1), _arg2(arg2), _f(f) {}
  37. public:
  38. TResult execute() {return _f(_arg1, _arg2);}
  39. private:
  40. T1 _arg1;
  41. T2 _arg2;
  42. Func _f;
  43. };
  44.  
  45. template<typename T1, typename T2, typename T3, typename Func>
  46. class _e3 : public executable{
  47. public:
  48. _e3(T1 arg1, T2 arg2, T3 arg3, Func f) : _arg1(arg1), _arg2(arg2), _arg3(arg3), _f(f) {}
  49. public:
  50. TResult execute() {return _f(_arg1, _arg2, _arg3);}
  51. private:
  52. T1 _arg1;
  53. T2 _arg2;
  54. T3 _arg3;
  55. Func _f;
  56. };
  57. };
  58.  
  59.  
  60. int main()
  61. {
  62. executor<int> etor(1, 1, add1);
  63. cout << etor.execute() << endl;
  64.  
  65. executor<double> etor2(1.0, 1.0, 1.0, add2);
  66. cout << etor2.execute() << endl;
  67.  
  68. return 0;
  69. }
  70.  
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
2
3