title: Pass tuple's content as variadic function arguments
body: <p>I play with C++0x for some time and now I want to use variadic templates and tuple to implement class "Task". I'm going to pass Task objects into newly created threads (using pthread). Task class will contain function pointer to function which should be called inside thread and arguments for this function, simplified code:</p>
  
  <pre><code>class TaskCaller
  {
      // ...
      virtual bool dispatch (void);
  };
  
  template&lt;typename ...T_arguments&gt; Task :
      public TaskCaller
  {
      public:
          // ...
          Task (bool           (*function) (T_arguments&amp;...),
                T_arguments... arguments) :
                    function_arguments_tuple (arguments...),
                    function (function)
          {
              // ...
          }
  
          bool dispatch (void)
          {
              return TupleUnpack&lt;sizeof ...(T_arguments)&gt;::unpack (this-&gt;function, this-&gt;function_arguments_tuple);
          }
  
      private:
          std::tuple&lt;T_arguments&amp;...&gt; function_arguments_tuple;
          bool                        (*function) (T_arguments...);
  };
  </code></pre>
  
  <p>And code which I use to unpack tuple into function arguments:</p>
  
  <pre><code>template&lt;unsigned int i&gt;  class TupleUnpack 
  {
      public:
          template&lt;typename T_return_type, typename ...T_tuple_arguments, typename ...T_function_arguments&gt;
              inline static T_return_type unpack (T_return_type                     (*function) (T_tuple_arguments&amp;...), 
                                                  std::tuple&lt;T_tuple_arguments...&gt;&amp; arguments_tuple,
                                                  T_function_arguments              ...function_arguments)
              {
                  return TupleUnpack&lt;i-1&gt;::unpack (function, arguments_tuple, std::get&lt;i-1&gt; (arguments_tuple), function_arguments...);
              }                       
  };
  
  template&lt;&gt; class TupleUnpack&lt;0&gt; 
  {
      public:
          template&lt;typename T_return_type, typename ...T_tuple_arguments, typename ...T_function_arguments&gt;
              inline static T_return_type unpack (T_return_type                     (*function) (T_tuple_arguments&amp;...), 
                                                  std::tuple&lt;T_tuple_arguments...&gt;&amp; arguments_tuple,
                                                  T_function_arguments              ...function_arguments)
              {
                  return function (function_arguments...);
              }          
  };
  </code></pre>
  
  <p>Use case:</p>
  
  <pre><code>bool task_function (Foo &amp;foo, Bar &amp;bar)
  {
      // ...
      return true;
  }
  
  void* thread_function (void* argument)
  {
      Task* task ((Task*) argument);
  
      task-&gt;dispatch ();
  
      delete task;
  
      pthread_exit (0);
  }
  
  void function (void)
  {
      Foo             foo (1, 2, 3);
      Bar             bar (1, 2, 3);
      Task&lt;Foo, Bar&gt;* task = new Task (task_function, std::move (foo) std::move (bar));
      pthread_t       thread_id;
  
      pthread_create (&amp;thread_id, task_function, task);
  }
  </code></pre>
  
  <p>I have not tested this code yet it's only and idea.</p>
  
  <p>Now I'm wondering how TupleUnpack class will impact the final code. According to my knowledge final implementation of Task::dispatch function  (after compiler parse templates) will be equivalent of:</p>
  
  <pre><code>template&lt;typename ...T_arguments&gt; static bool Task&lt;...T_arguments&gt;::dispatch (void)
  {
      return this-&gt;function (std::get&lt;0&gt; (this-&gt;function_arguments_tuple), std::get&lt;1&gt; (this-&gt;function_arguments_tuple), ..., std::get&lt;n&gt; (this-&gt;function_arguments_tuple));
  }
  </code></pre>
  
  <p>right? </p>
  
  <p>Moreover tuple itself and std::get() should "disappear" in final code and provide no run-time overhead <a href="http://www.boost.org/doc/libs/1_35_0/libs/tuple/doc/tuple_users_guide.html#performance" rel="nofollow">(according to Boost documentation)</a>.</p>
  
  <p>Maybe there's better way to solve my problem...</p>