fork(5) download
  1. #include <iostream>
  2.  
  3. template <typename F0, typename... F>
  4. class Composer2 {
  5. F0 f0_;
  6. Composer2<F...> tail_;
  7. public:
  8. Composer2(F0 f0, F... f) : f0_(f0), tail_(f...) {}
  9.  
  10. template <typename T>
  11. T operator() (const T& x) const {
  12. return f0_(tail_(x));
  13. }
  14. };
  15.  
  16. template <typename F>
  17. class Composer2<F> {
  18. F f_;
  19. public:
  20. Composer2(F f) : f_(f) {}
  21.  
  22. template <typename T>
  23. T operator() (const T& x) const {
  24. return f_(x);
  25. }
  26. };
  27.  
  28. template <typename... F>
  29. Composer2<F...> compose2(F... f) {
  30. return Composer2<F...>(f...);
  31. }
  32.  
  33. int f(int x) { return x + 1; }
  34. int g(int x) { return x * 2; }
  35. int h(int x) { return x - 1; }
  36.  
  37. int main() {
  38. std::cout << compose2(f, g, h)(42);
  39. return 0;
  40. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
83