title: Calling a function and passing parameters stored in a tuple?
body: <p>I want a class Foo to store a function pointer, which it got on construction, and to call this function at some point. I looked at these two questions for help:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/4105002/pass-tuples-content-as-variadic-function-arguments">Pass tuple&#39;s content as variadic function arguments</a></li>
  <li><a href="https://stackoverflow.com/questions/687490/how-do-i-expand-a-tuple-into-variadic-template-functions-arguments">How do I expand a tuple into variadic template function&#39;s arguments?</a></li>
  </ul>
  
  <p>And, based on the answers, came up with this code:</p>
  
  <pre><code>#include &lt;functional&gt;
  
  template &lt; int N, typename... ARGS &gt;
  struct apply_func {
     static void applyTuple( std::function&lt;void(ARGS...)&gt;&amp; f,
                            const std::tuple&lt;ARGS...&gt;&amp; t,
                            ARGS... args ) {
      apply_func&lt;N-1&gt;::applyTuple( f, t, std::get&lt;N-1&gt;( t ), args... );
    }
  };
  
  template &lt;typename... ARGS&gt;
  struct apply_func&lt;0,ARGS...&gt;
  {
    static void applyTuple( std::function&lt;void(ARGS...)&gt;&amp; f,
                            const std::tuple&lt;ARGS...&gt;&amp; /* t */,
                            ARGS... args ) {
      f( args... );
    }
  };
  
  template &lt; typename... ARGS &gt;
  void applyTuple( std::function&lt;void(ARGS...)&gt;&amp; f,
                   std::tuple&lt;ARGS...&gt; const&amp; t ) {
     apply_func&lt;sizeof...(ARGS), ARGS...&gt;::applyTuple( f, t );
  }
  
  template&lt;typename... ARGS&gt;
  class Foo
  {
      std::function&lt;void(ARGS...)&gt; m_f;
      std::tuple&lt;ARGS...&gt; *argument_pack;
  
      public:
      Foo(std::function&lt;void(ARGS...)&gt; f):m_f(f){}
      void operator()(ARGS... args);
      void run();
  
  };
  
  template&lt;typename... ARGS&gt;
  void Foo&lt;ARGS...&gt;::operator()(ARGS... args)
  {
      m_f(args...); // this works
  }
  
  template&lt;typename... ARGS&gt;
  void Foo&lt;ARGS...&gt;::run()
  {
      applyTuple&lt;ARGS...&gt;(m_f, *argument_pack); // this doesn't compile
  }
  
  void bar(int i, double d){}
  
  int main(void)
  {
      Foo&lt;int,double&gt; foo(bar);
      foo(1,1.0); // this works
      foo.run(); // this doesn't compile
  }
  </code></pre>
  
  <p>If you compile with ´g++ -std=c++0x´ the next to last line will give this error:</p>
  
  <pre><code>    test.cc: In function ‘void applyTuple(std::function&lt;void(ARGS ...)&gt;&amp;, const std::tuple&lt;_Elements ...&gt;&amp;) [with ARGS = {int, double}]’:
  test.cc:52:9:   instantiated from ‘void Foo&lt;ARGS&gt;::run() [with ARGS = {int, double}]’
  test.cc:61:17:   instantiated from here
  test.cc:27:8: error: no matching function for call to ‘apply_func&lt;2, int, double&gt;::applyTuple(std::function&lt;void(int, double)&gt;&amp;, const std::tuple&lt;int, double&gt;&amp;)’
  test.cc:27:8: note: candidate is:
  test.cc:6:19: note: static void apply_func&lt;N, ARGS&gt;::applyTuple(std::function&lt;void(ARGS ...)&gt;&amp;, const std::tuple&lt;_Elements ...&gt;&amp;, ARGS ...) [with int N = 2, ARGS = {int, double}]
  test.cc:6:19: note:   candidate expects 4 arguments, 2 provided
  test.cc: In static member function ‘static void apply_func&lt;N, ARGS&gt;::applyTuple(std::function&lt;void(ARGS ...)&gt;&amp;, const std::tuple&lt;_Elements ...&gt;&amp;, ARGS ...) [with int N = 2, ARGS = {int, double}]’:
  test.cc:27:8:   instantiated from ‘void applyTuple(std::function&lt;void(ARGS ...)&gt;&amp;, const std::tuple&lt;_Elements ...&gt;&amp;) [with ARGS = {int, double}]’
  test.cc:52:9:   instantiated from ‘void Foo&lt;ARGS&gt;::run() [with ARGS = {int, double}]’
  test.cc:61:17:   instantiated from here
  test.cc:9:9: error: no matching function for call to ‘apply_func&lt;1&gt;::applyTuple(std::function&lt;void(int, double)&gt;&amp;, const std::tuple&lt;int, double&gt;&amp;, const double&amp;, int&amp;, double&amp;)’
  test.cc:9:9: note: candidate is:
  test.cc:6:19: note: static void apply_func&lt;N, ARGS&gt;::applyTuple(std::function&lt;void(ARGS ...)&gt;&amp;, const std::tuple&lt;_Elements ...&gt;&amp;, ARGS ...) [with int N = 1, ARGS = {}]
  test.cc:6:19: note:   candidate expects 2 arguments, 5 provided
  test.cc: In static member function ‘static void apply_func&lt;N, ARGS&gt;::applyTuple(std::function&lt;void(ARGS ...)&gt;&amp;, const std::tuple&lt;_Elements ...&gt;&amp;, ARGS ...) [with int N = 1, ARGS = {}]’:
  test.cc:9:9:   instantiated from ‘static void apply_func&lt;N, ARGS&gt;::applyTuple(std::function&lt;void(ARGS ...)&gt;&amp;, const std::tuple&lt;_Elements ...&gt;&amp;, ARGS ...) [with int N = 2, ARGS = {int, double}]’
  test.cc:27:8:   instantiated from ‘void applyTuple(std::function&lt;void(ARGS ...)&gt;&amp;, const std::tuple&lt;_Elements ...&gt;&amp;) [with ARGS = {int, double}]’
  test.cc:52:9:   instantiated from ‘void Foo&lt;ARGS&gt;::run() [with ARGS = {int, double}]’
  test.cc:61:17:   instantiated from here
  test.cc:9:9: error: no matching function for call to ‘get(const std::tuple&lt;&gt;&amp;)’
  test.cc:9:9: note: candidates are:
  /usr/include/c++/4.6/utility:133:5: note: template&lt;long unsigned int _Int, class _Tp1, class _Tp2&gt; typename std::tuple_element&lt;_Int, std::pair&lt;_Tp1, _Tp2&gt; &gt;::type&amp; std::get(std::pair&lt;_Tp1, _Tp2&gt;&amp;)
  /usr/include/c++/4.6/utility:138:5: note: template&lt;long unsigned int _Int, class _Tp1, class _Tp2&gt; const typename std::tuple_element&lt;_Int, std::pair&lt;_Tp1, _Tp2&gt; &gt;::type&amp; std::get(const std::pair&lt;_Tp1, _Tp2&gt;&amp;)
  /usr/include/c++/4.6/tuple:531:5: note: template&lt;long unsigned int __i, class ... _Elements&gt; typename std::__add_ref&lt;typename std::tuple_element&lt;__i, std::tuple&lt;_Elements ...&gt; &gt;::type&gt;::type std::get(std::tuple&lt;_Elements ...&gt;&amp;)
  /usr/include/c++/4.6/tuple:538:5: note: template&lt;long unsigned int __i, class ... _Elements&gt; typename std::__add_c_ref&lt;typename std::tuple_element&lt;__i, std::tuple&lt;_Elements ...&gt; &gt;::type&gt;::type std::get(const std::tuple&lt;_Elements ...&gt;&amp;)
  </code></pre>
  
  <p>What am I missing? Thanks!</p>