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
  17. {
  18. return out_type () ;
  19. }
  20. };
  21.  
  22. template <class In, class Out, class Out2>
  23. auto operator>> (const Pipe <In, Out> &lhs, Pipe <Out, Out2> &rhs) -> Pipe <Out, Out2>&
  24. {
  25. rhs = lhs () ;
  26. return rhs ;
  27. }
  28.  
  29. template <class In, class Out>
  30. auto operator>> (const Pipe <In, Out> &lhs, Out &rhs) -> Out&
  31. {
  32. rhs = lhs () ;
  33. return rhs ;
  34. }
  35.  
  36. struct StringToInt : public Pipe <std::string, int>
  37. {
  38. StringToInt (const std::string &s = "") : Pipe <in_type, out_type> (s)
  39. {
  40. }
  41.  
  42. auto operator () () const -> out_type
  43. {
  44. return std::stoi (in_val) ;
  45. }
  46. };
  47.  
  48. struct IntSquare : public Pipe <int, int>
  49. {
  50. IntSquare (int n = 0) : Pipe <in_type, out_type> (n)
  51. {
  52. }
  53.  
  54. auto operator () () const -> out_type
  55. {
  56. return in_val * in_val ;
  57. }
  58. };
  59.  
  60. struct DivideBy42F : public Pipe <int, float>
  61. {
  62. DivideBy42F (int n = 0) : Pipe <in_type, out_type> (n)
  63. {
  64. }
  65.  
  66. auto operator () () const -> out_type
  67. {
  68. return static_cast <float> (in_val) / 42.0f ;
  69. }
  70. };
  71.  
  72. int main ()
  73. {
  74. float out = 0 ;
  75. StringToInt ("42") >> IntSquare () >> DivideBy42F () >> out ;
  76. std::cout << out << "\n" ;
  77.  
  78. return 0 ;
  79. }
  80.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:75:21: error: no match for ‘operator>>’ (operand types are ‘StringToInt’ and ‘IntSquare’)
  StringToInt ("42") >> IntSquare () >> DivideBy42F () >> out ;
                     ^
prog.cpp:75:21: note: candidates are:
prog.cpp:23:6: note: Pipe<Out, Out2>& operator>>(const Pipe<In, Out>&, Pipe<Out, Out2>&) [with In = std::basic_string<char>; Out = int; Out2 = int]
 auto operator>> (const Pipe <In, Out> &lhs, Pipe <Out, Out2> &rhs) -> Pipe <Out, Out2>&
      ^
prog.cpp:23:6: note:   no known conversion for argument 2 from ‘IntSquare’ to ‘Pipe<int, int>&’
prog.cpp:30:6: note: template<class In, class Out> Out& operator>>(const Pipe<In, Out>&, Out&)
 auto operator>> (const Pipe <In, Out> &lhs, Out &rhs) -> Out&
      ^
prog.cpp:30:6: note:   template argument deduction/substitution failed:
prog.cpp:75:35: note:   deduced conflicting types for parameter ‘Out’ (‘int’ and ‘IntSquare’)
  StringToInt ("42") >> IntSquare () >> DivideBy42F () >> out ;
                                   ^
stdout
Standard output is empty