title: C++11 does not deduce type when std::function or lambda functions are involved
body: <p>When I define this function,</p>
  
  <pre><code>template&lt;class A&gt;
  set&lt;A&gt; test(const set&lt;A&gt;&amp; input) {
      return input;
  }
  </code></pre>
  
  <p>I can call it using <code>test(mySet)</code> elsewhere in the code without having to explicitly define the template type. However, when I use the following function:</p>
  
  <pre><code>template&lt;class A&gt;
  set&lt;A&gt; filter(const set&lt;A&gt;&amp; input,function&lt;bool(A)&gt; compare) {
      set&lt;A&gt; ret;
      for(auto it = input.begin(); it != input.end(); it++) {
          if(compare(*it)) {
              ret.insert(*it);
          }
      }
      return ret;
  }
  </code></pre>
  
  <p>When I call this function using <code>filter(mySet,[](int i) { return i%2==0; });</code>
  I get the following error:</p>
  
  <blockquote>
    <p>error: no matching function for call to ‘filter(std::set&amp;, main()::)’</p>
  </blockquote>
  
  <p>However, all of these versions <em>do</em> work:</p>
  
  <pre><code>std::function&lt;bool(int)&gt; func = [](int i) { return i%2 ==0; };
  set&lt;int&gt; myNewSet = filter(mySet,func);
  
  set&lt;int&gt; myNewSet = filter&lt;int&gt;(mySet,[](int i) { return i%2==0; });
  
  set&lt;int&gt; myNewSet = filter(mySet,function&lt;bool(int)&gt;([](int i){return i%2==0;}));
  </code></pre>
  
  <p>Why is c++11 unable to guess the template type when I put the lambda function directly inside the expression without directly creating a <code>std::function</code>?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Per advice of Luc Danton in the comments, here is an alternative to the function I had earlier that does not need the templates to be passed explicitly.</p>
  
  <pre><code>template&lt;class A,class CompareFunction&gt;
  set&lt;A&gt; filter(const set&lt;A&gt;&amp; input,CompareFunction compare) {
      set&lt;A&gt; ret;
      for(auto it = input.begin(); it != input.end(); it++) {
          if(compare(*it)) {
              ret.insert(*it);
          }
      }
      return ret;
  }
  </code></pre>
  
  <p>This can be called by <code>set&lt;int&gt; result = filter(myIntSet,[](int i) { i % 2 == 0; });</code> without needing the template.</p>
  
  <p>The compiler can even guess the return types to some extent, using the new decltype keyword and using the new function return type syntax. Here is an example that converts a set to a map, using one filtering function and one function that generates the keys based on the values:</p>
  
  <pre><code>template&lt;class Value,class CompareType,class IndexType&gt;
  auto filter(const set&lt;Value&gt;&amp; input,CompareType compare,IndexType index) -&gt; map&lt;decltype(index(*(input.begin()))),Value&gt; {
      map&lt;decltype(index(*(input.begin()))),Value&gt; ret;
      for(auto it = input.begin(); it != input.end(); it++) {
          if(compare(*it)) {
              ret[index(*it)] = *it;
          }
      }
      return ret;
  }
  </code></pre>
  
  <p>It can also be called without using the template directly, as</p>
  
  <pre><code>map&lt;string,int&gt; s = filter(myIntSet,[](int i) { return i%2==0; },[](int i) { return toString(i); });
  </code></pre>