fork(1) download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. // Manual function definitions.
  5. std::function<double(double)> plus2(double a){
  6. return[a](double b){return a + b; };
  7. }
  8.  
  9. auto plus3(double a) {
  10. return [a](double b){ return plus2(a + b); };
  11. }
  12.  
  13. // -----
  14.  
  15. // Automatic function definitions.
  16. template<int N>
  17. auto plus(double a);
  18.  
  19. template<int N>
  20. auto plus(double a) {
  21. return [a](double b){ return plus<N - 1>(a + b); };
  22. }
  23.  
  24. template<>
  25. auto plus<1>(double a) {
  26. return a;
  27. }
  28.  
  29. // -----
  30.  
  31. int main() {
  32. std::cout << "Manual:\n"
  33. << plus2(1)(2) << ' '
  34. << plus3(1)(2)(3) << '\n'
  35. << "\nTemplate:\n"
  36. << plus<2>(1)(2) << ' '
  37. << plus<3>(1)(2)(3) << ' '
  38. << plus<4>(1)(2)(3)(4) << ' '
  39. << plus<5>(1)(2)(3)(4)(5) << '\n';
  40. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
Manual:
3 6

Template:
3 6 10 15