fork(1) download
  1. #include <cstdio>
  2.  
  3. template <typename T>
  4. struct unnamed_lambda
  5. {
  6. unnamed_lambda(T term) : captured_term(term) {}
  7.  
  8. template <typename A>
  9. unnamed_lambda operator()(A func);
  10.  
  11. T captured_term;
  12. };
  13.  
  14. struct terminal_lambda
  15. {
  16. template <typename A>
  17. unnamed_lambda<A> operator()(A term)
  18. {
  19. return unnamed_lambda<A>{term};
  20. }
  21. };
  22.  
  23. terminal_lambda terminal;
  24.  
  25. template <typename T>
  26. template <typename A>
  27. unnamed_lambda<T> unnamed_lambda<T>::operator()(A func)
  28. {
  29. return terminal(func(captured_term));
  30. }
  31.  
  32. struct Hello
  33. {
  34. FILE* operator()(FILE* s)
  35. {
  36. fprintf(s, "Hello\n");
  37. return s;
  38. }
  39. };
  40.  
  41. struct World
  42. {
  43. FILE* operator()(FILE* s)
  44. {
  45. fprintf(s, "World\n");
  46. return s;
  47. }
  48. };
  49.  
  50. int main()
  51. {
  52. Hello hello;
  53. World world;
  54. unnamed_lambda<FILE*> l1 = terminal(stdout);
  55. unnamed_lambda<FILE*> l2 = l1(hello);
  56. unnamed_lambda<FILE*> l3 = l2(world);
  57.  
  58. // same as:
  59. terminal(stdout)(hello)(world);
  60. }
  61.  
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
Hello
World
Hello
World