title: Wrap a function pointer in C++ with variadic template
body: <h1>The Question</h1>
  
  <p>I have a number of C++ functions <code>void f()</code>, <code>R g(T a)</code>, <code>S h(U a, V b)</code> and so on. I want to write a template function that accepts <code>f</code>, <code>g</code>, <code>h</code> and so on as a <em>template argument</em> and calls that function.</p>
  
  <p><em>ie</em> I want something like this:</p>
  
  <pre><code>template&lt;MagicStuff, WrappedFunction&gt;
  ReturnType wrapper(MagicallyCorrectParams... params)
  {
      extra_processing(); // Extra stuff that the wrapper adds
      return WrappedFunction(params);
  }
  ...
  wrapper&lt;f&gt;(); // calls f
  wrapper&lt;g&gt;(T()); // calls g
  wrapper&lt;h&gt;(U(), V()); // calls h
  </code></pre>
  
  <h1>Here's what I've tried so far:</h1>
  
  <h2>Solution 1</h2>
  
  <pre><code>template&lt;typename ReturnType, typename Args...&gt;
  ReturnType wrapper(ReturnType (*wrappee)(Args...), Args... args)
  {
      extra_processing();
      return wrappee(args...);
  }
  ...
  wrapper(f); // calls f OK
  wrapper(g, T()); // calls g OK
  wrapper(h, U(), V()); // calls h OK
  </code></pre>
  
  <p>This works but is unsatisfactory because in my case, I want the function pointer bound to the template instance. The function pointer is determinable statically at compile time and it is not desirable in my use case to have to pass it as a parameter at runtime.</p>
  
  <h2>Solution 2</h2>
  
  <pre><code>template&lt;
      typename ReturnType, typename Args...,
      ReturnType (*FuncPtr)(Args...)
  &gt;
  wrapper(Args... args)
  {
      extra_processing();
      return FuncPtr(args...);
  }
  ...
  wrapper&lt;void, f&gt;(); // calls f
  wrapper&lt;R, T, g&gt;(T()); // calls g
  wrapper&lt;S, U, V, h&gt;(U(), V()); // calls h
  </code></pre>
  
  <p>This works but is unsatisfactory because it is verbose. The return type and parameter types can be deduced from the function pointer itself. What would be perfect is a template specification so I can do <code>wrapper&lt;g&gt;(T())</code> as indicated above.</p>
  
  <p>Thanks for all help!</p>