title: How to overload an operator for composition of functionals in C++0x?
body: <p>Is there a way to overload, say the <code>&gt;&gt;</code> operator for function composition? The operator should work seamlessly on lambdas as well as <code>std::function</code>?</p>
  
  <p>Requirements:</p>
  
  <ul>
  <li>The solution should not include nested <code>bind</code> calls,</li>
  <li>the left operand can be of a functional type with an arbitrary number of parameters, and</li>
  <li>no more than one function object instance should be created.</li>
  </ul>
  
  <p>Here is a quick and dirty example that illustrates the desired behaviour:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;functional&gt;
  
  using namespace std;
  
  // An example of a quick and dirty function composition.
  // Note that instead of 'std::function' this operator should accept
  // any functional/callable type (just like 'bind').
  template&lt;typename R1, typename R2, typename... ArgTypes1&gt;
  function&lt;R2(ArgTypes1...)&gt; operator &gt;&gt; (
                  const function&lt;R1(ArgTypes1...)&gt;&amp; f1,
                  const function&lt;R2(R1)&gt;&amp; f2) {
      return [=](ArgTypes1... args){ return f2(f1(args...)); };
  }
  
  int main(int argc, char **args) {
      auto l1 = [](int i, int j) {return i + j;};
      auto l2 = [](int i) {return i * i;};
  
      function&lt;int(int, int)&gt; f1 = l1;
      function&lt;int(int)&gt; f2 = l2;
  
      cout &lt;&lt; "Function composition: " &lt;&lt; (f1 &gt;&gt; f2)(3, 5) &lt;&lt; endl;
  
      // The following is desired, but it doesn't compile as it is:
      cout &lt;&lt; "Function composition: " &lt;&lt; (l1 &gt;&gt; l2)(3, 5) &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>