title: What are the type conversion rules for parameters and return values of lambdas?
body: <p>I was recently surprised by the fact that lambdas can be assigned to <code>std::function</code>s with <em>slightly different</em> signatures. <em>Slightly different</em> meaning that return values of the lambda might be ignored when the <code>function</code> is specified to return <code>void</code>, or that the parameters might be references in the <code>function</code> but values in the lambda. </p>
  
  <p>See <a href="http://ideone.com/xXXvQz" rel="nofollow noreferrer">this example (ideone)</a> where I highlighted what I would suspect to be incompatible. I would think that the return value isn't a problem since you can always call a function and ignore the return value, but the conversion from a reference to a value looks strange to me: </p>
  
  <pre><code>int main() {
      function&lt;void(const int&amp; i)&gt; f;
      //       ^^^^ ^^^^^    ^
      f = [](int i) -&gt; int { cout&lt;&lt;i&lt;&lt;endl; return i; };
      //     ^^^    ^^^^^^
      f(2);
      return 0;
  }
  </code></pre>
  
  <p>The minor question is: Why does this code compile and work? The major question is: What are the general rules for type conversion of lambda parameters and return values when used together with <code>std::function</code>?</p>