fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. template <class In, class Out>
  5. struct Pipe
  6. {
  7. typedef In in_type ;
  8. typedef Out out_type ;
  9.  
  10. In in_val ;
  11.  
  12. Pipe (const in_type &in_val = in_type()) : in_val (in_val)
  13. {
  14. }
  15.  
  16. //virtual auto operator () () const -> out_type = 0 ;
  17.  
  18. virtual auto operator () () const -> out_type
  19. {
  20. return out_type () ;
  21. }
  22. };
  23.  
  24. template <class In, class Out, class Out2>
  25. auto operator>> (const Pipe <In, Out> &lhs, Pipe <Out, Out2> &rhs) -> Pipe <Out, Out2>&
  26. {
  27. rhs = lhs () ;
  28. return rhs ;
  29. }
  30.  
  31. template <class In, class Out, class Out2>
  32. auto operator>> (const Pipe <In, Out> &lhs, Pipe <Out, Out2> &&rhs) -> Pipe <Out, Out2>&
  33. {
  34. rhs = lhs () ;
  35. return rhs ;
  36. }
  37.  
  38. template <class In, class Out>
  39. auto operator>> (const Pipe <In, Out> &lhs, Out &rhs) -> Out&
  40. {
  41. rhs = lhs () ;
  42. return rhs ;
  43. }
  44.  
  45. template <class In, class Out>
  46. auto operator>> (const Pipe <In, Out> &lhs, Out &&rhs) -> Out&
  47. {
  48. rhs = lhs () ;
  49. return rhs ;
  50. }
  51.  
  52. struct StringToInt : public Pipe <std::string, int>
  53. {
  54. StringToInt (const std::string &s = "") : Pipe <in_type, out_type> (s)
  55. {
  56. }
  57.  
  58. auto operator () () const -> out_type
  59. {
  60. return std::stoi (in_val) ;
  61. }
  62. };
  63.  
  64. struct IntSquare : public Pipe <int, int>
  65. {
  66. IntSquare (int n = 0) : Pipe <in_type, out_type> (n)
  67. {
  68. }
  69.  
  70. auto operator () () const -> out_type
  71. {
  72. return in_val * in_val ;
  73. }
  74. };
  75.  
  76. struct DivideBy42F : public Pipe <int, float>
  77. {
  78. DivideBy42F (int n = 0) : Pipe <in_type, out_type> (n)
  79. {
  80. }
  81.  
  82. auto operator () () const -> out_type
  83. {
  84. return static_cast <float> (in_val) / 42.0f ;
  85. }
  86. };
  87.  
  88. int main ()
  89. {
  90. float out = 0 ;
  91. StringToInt ("42") >> IntSquare () >> DivideBy42F () >> out ;
  92. std::cout << out << "\n" ;
  93.  
  94. return 0 ;
  95. }
  96.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
42