fork download
  1. #include <iostream>
  2.  
  3. struct fact {
  4. template<typename F> unsigned operator()(unsigned i, F&& f) {
  5. if(i == 0)
  6. return 1;
  7. return i * f(i - 1);
  8. }
  9. };
  10. struct doubler {
  11. template<typename F> unsigned operator()(unsigned i, F&& f) {
  12. return 2 * f(i);
  13. }
  14. };
  15. template<typename F> struct y_combinator_ref {
  16. F& f;
  17. y_combinator_ref(F& farg)
  18. : f(farg) {}
  19. template<typename... Args> auto operator()(Args&&... args)
  20. -> decltype(f(std::forward<Args>(args)..., y_combinator_ref<F>(f))) {
  21. return f(std::forward<Args>(args)..., y_combinator_ref<F>(f));
  22. }
  23. };
  24. template<typename F> struct y_combinator {
  25. y_combinator(F farg)
  26. : f(std::move(farg)) {}
  27. F f;
  28. template<typename... Args> auto operator()(Args&&... args)
  29. -> decltype(f(std::forward<Args>(args)..., y_combinator_ref<F>(f))) {
  30. return f(std::forward<Args>(args)..., y_combinator_ref<F>(f));
  31. }
  32. };
  33. template<typename F> y_combinator<F> y_combine(F f) {
  34. return y_combinator<F>(std::move(f));
  35. }
  36. int main() {
  37. auto result = y_combine(fact());
  38. std::cout << result(5);
  39. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:28:58: sorry, unimplemented: use of 'type_pack_expansion' in template
prog.cpp:38:26: error: no match for call to '(y_combinator<fact>) (int)'
stdout
Standard output is empty