title: How to make C++11 functions taking function<> parameters accept lambdas automatically
body: <p>C++11 has both lambda's and std::function&lt;>, but unfortunately, they have different types.
  One consequence is that one cannot directly use lambda's in higher order functions such as map in lisp. For example, in the following code </p>
  
  <pre><code> #include &lt;vector&gt;
   #include &lt;functional&gt;
   using namespace std;
  
   template &lt;typename A,typename B&gt; 
   vector&lt;B&gt; map(std::function&lt;B (A)&gt; f, vector&lt;A&gt; arr) {
         vector&lt;B&gt; res;
         for (int i=0;i&lt;arr.size();i++) res.push_back(f(arr[i]));
         return res;
  }
  
  int main () {
      vector&lt;int&gt; a = {1,2,3};
      map([](int x) -&gt; int { return x;},a); //not OK
  
      auto id_l = [](int x) -&gt; int { return x;};
      map(id_l,a); //not OK;
  
      function&lt;int (int)&gt; id_f = id_l;
      map(id_f,a); //OK
  return 0;
  }
  </code></pre>
  
  <p>, directly using lambda as in line 2 of main() won't work. <code>g++ -std=c++11 testfunc.cpp</code> returns `... testfunc.cpp:14:37: note:   'main()::__lambda0' is not derived from 'std::function'.</p>
  
  <p>C++11 type inferencing fails as well, as you can see if one stores the lambda to an auto variable and then use it, the type information is still lost, probably due to type erasure and reasons of small performance penalty (as I was told: <a href="https://stackoverflow.com/questions/11628765/why-do-lambda-functions-in-c11-not-have-function-types">why do lambda functions in c++11 not have function&lt;&gt; types?</a>).</p>
  
  <p>What does work is to store the lambda in a std:function&lt;> typed variable and use that variable. This is rather inconvenient and kind of defeats the purpose of using lambda's in functional programming in C++11. For example, one cannot manipulate lambda's in place with stuff like bind or flip, and instead has to store the lambda to a variable first.</p>
  
  <p>My question is, is it possible (and how) to overcome this issue and make line#2 of main() legal, e.g. by overwriting some typecast operators? (Of course, this means I don't care about the small performance penalty involved with using/not using type erasure.)</p>
  
  <p>thanks in advance.</p>
  
  <p>--- EDIT ---</p>
  
  <p>To clarify, the reason I use <code>std::function</code> rather than a generic type parameter for the functional parameter is that <code>std::function</code> has exact type information, while a generic type parameter as in <code>template &lt;typename F&gt; map(F f, ...)</code> contains no type information. Also, as I finally figured out, each lambda is its own type. So type erasure wasn't even a issue in the incompatibility between a lambda and its matching <code>std::function</code> object.</p>
  
  <p>---Update---</p>
  
  <p>There are already two answers about how to make the map function above work or how to improve them. Just to clarify. My question isn't about how to make map work. There are plenty of other use cases involving using the std::function&lt;> typed parameters, which I think can at least make the code more readable and make type inferencing easy. The answers so far are about how not to use std::function&lt;> as parameters. My question was about how to make such a function (with std::function&lt;> typed parameters) accept lambda's automatically.</p>
  
  <p>-- Update 2 ---</p>
  
  <p>In response to comments, here is a example of practical case where the type information in std::function&lt;> COULD be useful. Suppose we want to implement a C++ equivalent of <code>fold_right : ('a -&gt; 'b -&gt; 'b) -&gt; 'a list -&gt; 'b -&gt; 'b</code> in OCaml (<a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html" rel="nofollow noreferrer">http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html</a>).</p>
  
  <p>With std::function&lt;>, one can do</p>
  
  <pre><code> //approach#1
   template &lt;typename A,typename B&gt; 
   B fold_right(std::function&lt;B (A, B)&gt; f, vector&lt;A&gt; arr, B b) {
       ...
   }
  </code></pre>
  
  <p>It is clear from above what <code>f</code> is, and what it can or cannot take. Maybe, one can also use</p>
  
  <pre><code> //approach#2
   template &lt;typename A,typename B, typename F&gt; 
   auto fold_right2(F f, vector&lt;A&gt; arr, B b) -&gt; decltype(f(???)) {
        ...
   }
  </code></pre>
  
  <p>But, this is becoming kind of ugly as you try to figure out what to put in the <code>decltype</code>. Also, what exactly does <code>f</code> take, and what's the correct way to use <code>f</code>? From the point view of readability, I guess the reader of the code can only figure out what is f (a function or scalar) and the signature of f by INTERPRETING the implementation in the function body. </p>
  
  <p>That is what I don't like and that's where my question comes from. How to make approach#1 work conveniently. For example, if <code>f</code> represents addition of two numbers, approach#1 works if you create a function object first:</p>
  
  <pre><code>std::function&lt;int (int, int)&gt; add = [](int x, int y) -&gt; int { return x + y; }
  fold_right(add,{1,2,3},0);
  </code></pre>
  
  <p>Efficiency issues aside, the above code is inconvenient BECAUSE std::function cannot accept lambda's. So, </p>
  
  <pre><code>fold_right([](int x, int y) -&gt; int { return x + y; },{1,2,3},0);
  </code></pre>
  
  <p>will not work currently in C++11. My question is specifically about if it is possible to make functions like <code>fold_right</code> defined above accept lambda's directly. Maybe it's too much to hope for. I hope this clarifies the question.</p>