title: How to create a variadic template function with `std::function` as a function parameter?
body: <p>How can I create a variadic template function with <code>std::function</code> as a function parameter that accepts a variadic number of arguments? I tried to reduce the problem to a MWE:</p>
  
  <pre><code>#include &lt;functional&gt;
  
  template &lt;class T&gt; void run(std::function&lt;void(T *)&gt; fun, T *obj) { fun(obj); }
  
  template &lt;class T, class... Args&gt;
  void run_variadic(std::function&lt;void(T *, Args...)&gt; fun, T *obj, Args... args) {
    fun(obj, args...);
  }
  
  struct Foo {
    void bar() {}
  };
  
  int main() {
    Foo foo;
    std::function&lt;void(Foo *)&gt; fun = &amp;Foo::bar;
  
    run(fun, &amp;foo);                     // works
    run&lt;Foo&gt;(&amp;Foo::bar, &amp;foo);          // works
    run_variadic(fun, &amp;foo);            // works
    run_variadic&lt;Foo&gt;(&amp;Foo::bar, &amp;foo); // does not compile
  }
  </code></pre>
  
  <p>It seems like the mere presence of the variadic template parameter in <code>run_variadic</code> makes it impossible to directly call it with a member function pointer. <code>clang</code>'s error message is as follows:</p>
  
  <pre><code>main.cpp:21:3: error: no matching function for call to 'run_variadic'
    run_variadic&lt;Foo&gt;(&amp;Foo::bar, &amp;foo); // does not compile
    ^~~~~~~~~~~~~~~~~
  main.cpp:6:6: note: candidate template ignored: could not match 'function&lt;void (Foo *, type-parameter-0-1...)&gt;' against 'void (Foo::*)()'
  void run_variadic(std::function&lt;void(T *, Args...)&gt; fun, T *obj, Args&amp;&amp;... args) {
       ^
  1 error generated.
  </code></pre>
  
  <p>Any suggestions on how I can fix <code>run_variadic</code> so that I do not have to go through the extra <code>std::function</code> object?</p>
  
  <p><strong>Background</strong></p>
  
  <p>I have a class hierarchy as</p>
  
  <pre><code>template &lt;class T&gt; class Abstract { ... };
  class UnrelatedStuff { ... };
  class Derived : public Abstract&lt;UnrelatedStuff&gt; { ... };
  </code></pre>
  
  <p>There are multiple <code>Derived</code> classes that all have to implement one or more methods to loop over a range of elements. The loop looks something like</p>
  
  <pre><code>#pragma omp parallel for
  for (ZFSId i = begin; i != end; ++i) {
    callMemFun(i, and, other, args);
  }
  </code></pre>
  
  <p>All loops should be OpenMP-accelerated. I want the accelerator stuff factored out and not repeated in each method of <code>Derived</code> that uses a loop, so that I only have to change one place if e.g. OpenMP would switch to OpenACC.</p>
  
  <p>Thus I am looking for a way to put the loop (and its decoration) in its own function. Moving it to the <code>Abstract</code> base class is not an option either, since the loops are performance-critical and I cannot have an abstract function call in each loop iteration.</p>