title: How to handle placeholders in variadic templates for a threadpool
body: <p>I'm implementing a threadPool with waitable tasks. The threadPool saves tasks in a threadsafe deque, here <code>threadsafeQueue&lt;function_wrapper&gt;</code>. Tasks are callable objects where almost all of the arguments are bound with <code>std::bind</code> except of the last. I started with the implementation from Anthony Williams' Concurrency In Action book (Listing 9.2). I have difficulties implementing the <code>function_wrapper</code> and the variadic template function <code>submit</code> so that when a task is popped out from the deque <code>try_pop(task)</code> and executed <code>task()</code> I still can set the last argument like <code>task(myLastArgument)</code>.</p>
  
  <p>This is Anthony Williams' function wrapper:</p>
  
  <pre><code>class function_wrapper
  {
      struct impl_base {
          virtual void call() = 0;
          virtual ~impl_base() {}
      };
  
      std::unique_ptr&lt;impl_base&gt; impl;
  
      template&lt;typename F&gt;
      struct impl_type : impl_base
      {
          F f_;
          impl_type(F&amp;&amp; f)
          : f_(std::move(f))
          {}
  
          void call() { f_(); }
      };
  
  public:
  
      template&lt;typename F&gt;
      function_wrapper(F&amp;&amp; f)
      : impl(new impl_type&lt;F&gt;(std::move(f)))
      {}
  
      void operator() () { impl-&gt;call(); }
  
      function_wrapper() = default;
  
      function_wrapper(function_wrapper&amp;&amp; other)
      : impl(std::move(other.impl))
      {}
  
      function_wrapper&amp; operator=(function_wrapper&amp;&amp; other)
      {
          impl = std::move(other.impl);
          return *this;
      }
  
      function_wrapper(const function_wrapper&amp;) = delete;
      function_wrapper(function_wrapper&amp;) = delete;
      function_wrapper&amp; operator=(const function_wrapper&amp;) = delete;
  };
  </code></pre>
  
  <p>This is the abbreviated threadPool:</p>
  
  <pre><code>template &lt;typename T&gt;
  class threadPool
  {
  private:
      threadsafeQueue&lt;function_wrapper&gt; workQueue_;   
      std::atomic_bool done_;
      std::vector&lt;std::thread&gt; threads_;
  
      void workerThread(unsigned long thread);
  
          T threadData_; 
  
      template&lt;typename FunctionType&gt; 
      auto submit(FunctionType&amp;&amp; f)
      {
          typedef typename std::result_of&lt;FunctionType()&gt;::type resultType;
          std::packaged_task&lt;resultType()&gt; task(std::move(f));
          std::future&lt;resultType&gt; result(task.get_future());
          workQueue_.push(std::move(task));
  
          return result;
      }
  };
  
  
  template &lt;typename T&gt;
  void threadPool&lt;T&gt;::workerThread(unsigned long thread)
  {
      while (!done_)
      {
          function_wrapper task;
  
          if(workQueue_.try_pop(task))
          {
              task();
          }
          else
          {
              std::this_thread::yield();
          }
  
      }
  }
  </code></pre>
  
  <p>With the shown implementation I can submit callable objects to the pool. The variadic template function <code>submit</code> creates a <code>std::packaged_task&lt;resultType()&gt;</code> and puts it on the deque. The worker threads run <code>workerThread(unsigned long thread)</code> and do there work by popping out the tasks previously inserted by the main thread and executing them with <code>task()</code>.</p>
  
  <p>A simple example for a main function:</p>
  
  <pre><code>int main()
  {
      threadPool&lt;double&gt; pool(5UL);
  
      auto fut = pool.submit(std::bind((void(*) (int,int))doTheWork,11,22));
  
      fut.wait();
  }
  </code></pre>
  
  <p>What I would like to have is a submit call in my main function like:</p>
  
  <pre><code>auto fut = pool.submit(std::bind((void(*) (int,int))doTheWork,11,std::placeholders::_1));
  </code></pre>
  
  <p>The task execution inside my thread pool should then look like:</p>
  
  <pre><code>task(thread);
  </code></pre>
  
  <p>How do I have to change the variadic template function <code>submit</code> and the <code>function_wrapper</code> to achieve my goal?</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>I tried the function wrapper implementation from:</p>
  
  <p><a href="https://stackoverflow.com/questions/30854070/return-values-for-active-objects/30854998#30854998">Return values for active objects</a></p>
  
  <p>Now the function wrapper is moveable only and can take arguments, thanks to Yakk. But the problem now is that the new function wrapper can not handle <code>std::bind()</code> with <code>std::placeholders</code>. For example I can define a task with the new wrapper like</p>
  
  <pre><code>myWrapper&lt;void(int,int)&gt; task1 = doTheWork;
  task1(66,77);
  </code></pre>
  
  <p>but I can not bind function arguments in a flexible way:</p>
  
  <pre><code>myWrapper&lt;void()&gt; task2 = std::bind(doTheWork, 66, 77); // ERROR 
  </code></pre>
  
  <p>With the error in VS2019:</p>
  
  <pre><code>error C2440:  'initializing': cannot convert from 'std::_Binder&lt;std::_Unforced,void (__cdecl *)(int,int),int,int&gt;' to 'myWrapper&lt;void (void)&gt;'
  message :  No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
  </code></pre>
  
  <p>Or when I try it with <code>std::placeholders</code>:</p>
  
  <pre><code>myWrapper&lt;void(int)&gt; task2 = std::bind(doTheWork, 66, std::placeholders::_1); // ERROR
  task2(77);
  </code></pre>
  
  <p>I get the following error in VS2019:</p>
  
  <pre><code>error C2338:  tuple index out of bounds
  message :  see reference to class template instantiation 'std::tuple_element&lt;0,std::tuple&lt;&gt;&gt;' being compiled
  message :  see reference to alias template instantiation 'std::tuple_element_t&lt;0,std::tuple&lt;&gt;&gt;' being compiled
  message :  see reference to function template instantiation 'const tuple_element&lt;_Index,std::tuple&lt;_Rest...&gt;&gt;::type &amp;&amp;std::get(const std::tuple&lt;_Rest...&gt; &amp;&amp;) noexcept' being compiled
  message :  see reference to class template instantiation 'std::result_of&lt;const F &amp;(void)&gt;' being compiled
  message :         with
  message :         [
  message :             F=std::_Binder&lt;std::_Unforced,void (__cdecl *)(int,int),int,const std::_Ph&lt;1&gt; &amp;&gt;
  message :         ]
  message :  see reference to alias template instantiation 'std::result_of_t&lt;const std::_Binder&lt;std::_Unforced,void (__cdecl *)(int,int),int,const std::_Ph&lt;1&gt; &amp;&gt;&amp;(void)&gt;' being compiled
  message :  see reference to class template instantiation 'std::integral_constant&lt;bool,false&gt;' being compiled
  message :  see reference to class template instantiation 'std::disjunction&lt;_Traits...&gt;' being compiled
  error C2440:  'initializing': cannot convert from 'std::_Binder&lt;std::_Unforced,void (__cdecl *)(int,int),int,const std::_Ph&lt;1&gt; &amp;&gt;' to 'myWrapper&lt;void (void)&gt;'
  message :  No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
  </code></pre>
  
  <p>However with <code>std::function</code> it´s possible:</p>
  
  <pre><code>std::function&lt;void(int)&gt; task3 = std::bind(doTheWork, 66, std::placeholders::_1);
  task3(77);
  </code></pre>
  
  <p>Any hints how to implement a movable only function wrapper with flexible arguments? The aim is still to use it inside a thread pool where the function wrapper is pased to a <code>std::packaged_task</code>.</p>