title: Is it possible to capture a variable number of parameters in a lambda?
body: <p>Consider the following set of examples. </p>
  
  <ol>
  <li>The function <code>takeOnlyVoidFunction</code> takes a function with zero arguments and simply executes it. </li>
  <li>The function <code>takeVariableArguments</code> takes a variable number of arguments and executes the function using the arguments.</li>
  <li>The function <code>captureVariableArgs</code> attempts to convert the second function into a lambda form that is acceptable by the first function, but it does not compile. </li>
  </ol>
  
  <p><strong>How can I make the function <code>captureVariableArgs</code> compile and exhibit the correct behavior of converting a function with a variable number of arguments into a closure with no arguments?</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;functional&gt;
  
  void takeOnlyVoidFunction(std::function&lt;void()&gt; task) {
      task();
  }
  
  template&lt;typename _Callable, typename... _Args&gt;
      void takeVariableArguments(_Callable&amp;&amp; __f, _Args&amp;&amp;... __args) {
       __f(__args...);
  }
  
  // How can I make this function compile?
  template&lt;typename _Callable, typename... _Args&gt;
      void captureVariableArgs(_Callable&amp;&amp; __f, _Args&amp;&amp;... __args) {
      takeOnlyVoidFunction([=]() { __f(__args...);});
  }
  
  void normalFunction(int a, int b) {
      printf("I am a normal function which takes params (%d,%d)\n", a, b);
  }
  
  int main() {
      int a = 7;
      int b = 8;
      takeVariableArguments(normalFunction, a, b);
      takeOnlyVoidFunction([=](){ normalFunction(a,b);});
      captureVariableArgs(normalFunction, a, b);
  }
  </code></pre>
  
  <p>I'm running <code>gcc 4.9.2</code>. Here is the compiler error I see.</p>
  
  <pre><code>g++ -std=c++11    Test.cc   -o Test
  Test.cc: In instantiation of ‘captureVariableArgs(_Callable&amp;&amp;, _Args&amp;&amp; ...)::&lt;lambda()&gt; [with _Callable = void (&amp;)(int, int); _Args = {int&amp;, int&amp;}]’:
  Test.cc:16:38:   required from ‘struct captureVariableArgs(_Callable&amp;&amp;, _Args&amp;&amp; ...) [with _Callable = void (&amp;)(int, int); _Args = {int&amp;, int&amp;}]::&lt;lambda()&gt;’
  Test.cc:16:50:   required from ‘void captureVariableArgs(_Callable&amp;&amp;, _Args&amp;&amp; ...) [with _Callable = void (&amp;)(int, int); _Args = {int&amp;, int&amp;}]’
  Test.cc:28:45:   required from here
  Test.cc:16:34: error: variable ‘__f’ has function type
       takeOnlyVoidFunction([=]() { __f(__args...);});
                                    ^
  Test.cc:16:34: error: variable ‘__f’ has function type
  Test.cc: In instantiation of ‘struct captureVariableArgs(_Callable&amp;&amp;, _Args&amp;&amp; ...) [with _Callable = void (&amp;)(int, int); _Args = {int&amp;, int&amp;}]::&lt;lambda()&gt;’:
  Test.cc:16:50:   required from ‘void captureVariableArgs(_Callable&amp;&amp;, _Args&amp;&amp; ...) [with _Callable = void (&amp;)(int, int); _Args = {int&amp;, int&amp;}]’
  Test.cc:28:45:   required from here
  Test.cc:16:34: error: field ‘captureVariableArgs(_Callable&amp;&amp;, _Args&amp;&amp; ...) [with _Callable = void (&amp;)(int, int); _Args = {int&amp;, int&amp;}]::&lt;lambda()&gt;::&lt;__f capture&gt;’ invalidly declared function type
  In file included from Test.cc:2:0:
  /usr/include/c++/4.9/functional:2418:7: error: ‘std::function&lt;_Res(_ArgTypes ...)&gt;::function(_Functor) [with _Functor = captureVariableArgs(_Callable&amp;&amp;, _Args&amp;&amp; ...) [with _Callable = void (&amp;)(int, int); _Args = {int&amp;, int&amp;}]::&lt;lambda()&gt;; &lt;template-parameter-2-2&gt; = void; _Res = void; _ArgTypes = {}]’, declared using local type ‘captureVariableArgs(_Callable&amp;&amp;, _Args&amp;&amp; ...) [with _Callable = void (&amp;)(int, int); _Args = {int&amp;, int&amp;}]::&lt;lambda()&gt;’, is used but never defined [-fpermissive]
         function&lt;_Res(_ArgTypes...)&gt;::
         ^
  </code></pre>
  
  <hr>
  
  <p><strong>Update</strong>: A more minimal example demonstrating this problem.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  // How can I make this function compile?
  template&lt;typename _Callable&gt;
  void captureVariableArgs(_Callable&amp;&amp; __f) {
      takeOnlyVoidFunction( [=]{ __f(); } );
  }
  
  void normalFunction() {
      printf("I am a normal function\n");
  }
  
  int main(){
      captureVariableArgs(normalFunction);
  }
  </code></pre>