title: Variadic templates, type deduction and std::function
body: <p>I'm trying to make a template function to which is possible to pass some other function with any type and number of parameters and bind it to a <code>std::function</code>. I managed to do this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;functional&gt;
  
  int foo(int bar)
  {
    std::cout &lt;&lt; bar &lt;&lt; std::endl;
    return bar;
  }
  
  template &lt;typename Ret, typename... Args&gt;
  std::function&lt;Ret (Args...)&gt; func(std::function&lt;Ret (Args...)&gt; f)
  {
    return f;
  }
  
  int main()
  {
    //auto barp = func(foo); // compilation error
    auto bar  = func(std::function&lt;void (int)&gt;(foo));
  
    bar (0); // prints 0
  }
  </code></pre>
  
  <p>I would like to just call <code>auto barp = func(foo);</code> and have the types deduced, but this line gives the following compilation errors:</p>
  
  <pre><code>error: no matching function for call to ‘func(void (&amp;)(int))’
      auto barp = func(foo);
                          ^
  note: candidate is:
  note: template&lt;class Ret, class ... Args&gt; std::function&lt;_Res(_ArgTypes ...)&gt; func(std::function&lt;_Res(_ArgTypes ...)&gt;)
   std::function&lt;Ret (Args...)&gt; func(std::function&lt;Ret (Args...)&gt; f)
                                ^
  note:   template argument deduction/substitution failed:
  note:   mismatched types ‘std::function&lt;_Res(_ArgTypes ...)&gt;’ and ‘int (*)(int)’
     auto barp = func(foo);
                         ^
  </code></pre>
  
  <p>Why is it trying to match <code>std::function&lt;_Res(_ArgTypes ...)&gt;</code> with <code>int (*)(int)</code>? I feel I should get the compiler somehow to expand <code>_Res(_ArgTypes ...)</code> to <code>int(int)</code>, but how?</p>