title: c++11: How to write a wrapper function to make `std::function` objects
body: <p>I am trying to write a wrapper <code>make_function</code>, which like <code>std::make_pair</code> can create a <code>std::function</code> object out of suitable callable objects. </p>
  
  <p>Just like <code>make_pair</code>, for a function pointer <code>foo</code>, <code>auto f0 = make_function(foo);</code> creates a <code>std::function</code> function object <code>f0</code> of the right type signature.
  Just to clarify, I don't mind occasionally giving type parameters to <code>make_function</code> in case it is difficult (or impossible) to deduce the type entirely from the parameters.</p>
  
  <p>What I came up with so far (code below) works fine for lambdas, some function pointers, and functors (I didn't consider volatiles). But I couldn't get it work for <code>std::bind</code> or <code>std::bind&lt;R&gt;</code> results. In the code below</p>
  
  <pre><code>auto f2 = make_function(std::bind(foo,_1,_2,_3)); //not OK
  </code></pre>
  
  <p>wouldn't compile/work, with gcc 4.8.1. I am guessing that I didn't capture the <code>operator()</code> for the <code>bind</code> result correctly, but I am not sure how to fix it.</p>
  
  <p>Any help on how to fix this case or improvement in other corner cases is appreciated.</p>
  
  <p>My question is, of course, how to fix the error in the example below.</p>
  
  <p>For background, one of the cases I use this wrapper can be found at this question: <a href="https://stackoverflow.com/questions/20722918/how-to-make-c11-functions-taking-function-parameters-accept-lambdas-automati/">How to make C++11 functions taking function&lt;&gt; parameters accept lambdas automatically</a>. If you do not approve the use of <code>std::function</code> or my specific way of using it, please leave your comments in that post, and discuss technical issues here.</p>
  
  <p><strong>--- EDIT ---</strong></p>
  
  <p>From some of the comments, I learned that it's because of the ambiguity issue (ambiguity of the function call operator() of <code>std::bind</code> results). As pointed out by @Mooing Duck's answer, the solution is to give the parameter types explicitly. I have updated the code to combine the three functions in @Mooing Duck's answer (with slight change of type parameters), so that the <code>make_function</code> wrapper can now handle/type-deduce unambiguous cases as before, and allow specification of complete type signature when there is ambiguity.</p>
  
  <p>(My original code for the unambiguous cases is at: <a href="https://stackoverflow.com/a/21665705/683218">https://stackoverflow.com/a/21665705/683218</a> and can be tested at: <a href="https://ideone.com/UhAk91" rel="nofollow noreferrer">https://ideone.com/UhAk91</a>):</p>
  
  <pre><code>#include &lt;functional&gt;
  #include &lt;utility&gt;
  #include &lt;iostream&gt;
  #include &lt;functional&gt;
  using namespace std;
  
  // For generic types that are functors, delegate to its 'operator()'
  template &lt;typename T&gt;
  struct function_traits
    : public function_traits&lt;decltype(&amp;T::operator())&gt;
  {};
  
  // for pointers to member function
  template &lt;typename ClassType, typename ReturnType, typename... Args&gt;
  struct function_traits&lt;ReturnType(ClassType::*)(Args...) const&gt; {
    enum { arity = sizeof...(Args) };
    typedef function&lt;ReturnType (Args...)&gt; f_type;
  };
  
  // for pointers to member function
  template &lt;typename ClassType, typename ReturnType, typename... Args&gt;
  struct function_traits&lt;ReturnType(ClassType::*)(Args...) &gt; {
    enum { arity = sizeof...(Args) };
    typedef function&lt;ReturnType (Args...)&gt; f_type;
  };
  
  // for function pointers
  template &lt;typename ReturnType, typename... Args&gt;
  struct function_traits&lt;ReturnType (*)(Args...)&gt;  {
    enum { arity = sizeof...(Args) };
    typedef function&lt;ReturnType (Args...)&gt; f_type;
  };
  
  template &lt;typename L&gt; 
  static typename function_traits&lt;L&gt;::f_type make_function(L l){
    return (typename function_traits&lt;L&gt;::f_type)(l);
  }
  
  //handles bind &amp; multiple function call operator()'s
  template&lt;typename ReturnType, typename... Args, class T&gt;
  auto make_function(T&amp;&amp; t) 
    -&gt; std::function&lt;decltype(ReturnType(t(std::declval&lt;Args&gt;()...)))(Args...)&gt; 
  {return {std::forward&lt;T&gt;(t)};}
  
  //handles explicit overloads
  template&lt;typename ReturnType, typename... Args&gt;
  auto make_function(ReturnType(*p)(Args...))
      -&gt; std::function&lt;ReturnType(Args...)&gt; {
    return {p};
  }
  
  //handles explicit overloads
  template&lt;typename ReturnType, typename... Args, typename ClassType&gt;
  auto make_function(ReturnType(ClassType::*p)(Args...)) 
      -&gt; std::function&lt;ReturnType(Args...)&gt; { 
    return {p};
  }
  
  // testing
  using namespace std::placeholders;
  
  int foo(int x, int y, int z) { return x + y + z;}
  int foo1(int x, int y, int z) { return x + y + z;}
  float foo1(int x, int y, float z) { return x + y + z;}
  
  int main () {
    //unambuiguous
    auto f0 = make_function(foo);
    auto f1 = make_function([](int x, int y, int z) { return x + y + z;});
    cout &lt;&lt; make_function([](int x, int y, int z) { return x + y + z;})(1,2,3) &lt;&lt; endl;
  
    int first = 4;
    auto lambda_state = [=](int y, int z) { return first + y + z;}; //lambda with states
    cout &lt;&lt; make_function(lambda_state)(1,2) &lt;&lt; endl;
  
    //ambuiguous cases
    auto f2 = make_function&lt;int,int,int,int&gt;(std::bind(foo,_1,_2,_3)); //bind results has multiple operator() overloads
    cout &lt;&lt; f2(1,2,3) &lt;&lt; endl;
    auto f3 = make_function&lt;int,int,int,int&gt;(foo1);     //overload1
    auto f4 = make_function&lt;float,int,int,float&gt;(foo1); //overload2
  
    return 0;
  }
  </code></pre>
  
  <p><a href="http://ideone.com/08SJrv" rel="nofollow noreferrer">Ideone</a></p>