title: How do I create a queue that holds boost::packaged_task<> with functions that return arbitrary types?
body: <p>I'm trying to construct a work queue of functions that need to be executed by one thread and can be fed by many threads. To accomplish this, I was planning on using the boost::packaged_task and boost::unique_future. The idea would be you would do:</p>
  
  <p>Foo value = queue.add(myFunc).get();</p>
  
  <p>which would block, until the function is executed. So queue.add(...) takes in a boost::function, and returns a boost::unique_future. Internally it then creates a boost::packaged_task using the boost::function for its constructor.</p>
  
  <p>The problem I'm running into is that boost::function&lt;...> won't be the same every time. Specifically, the return value for it will change (the functions, however, will never take any parameters). Thus, I have to have an add function that looks something like:</p>
  
  <pre><code>template &lt;typename ResultType&gt;
  boost::unique_future&lt;ResultType&gt; add(boost::function&lt;ResultType ()&gt; f) {
     boost::packaged_task&lt;boost::function&lt;ResultType ()&gt; &gt; task(f);
     queue.push_back(task);
     return task.get_future();
  }
  </code></pre>
  
  <p>Okay, that doesn't seem too bad, but then I ran into the problem of how to define 'queue'. I think I have no choice but to use boost::any, since the types will not be constant:</p>
  
  <pre><code>std::list&lt;boost::any&gt; queue; // note: I'm not concerned with thread-safety yet
  </code></pre>
  
  <p>But then I run into a problem when I try to implement my executeSingle (takes just a single item off the queue to execute):</p>
  
  <pre><code>void executeSingle() {
      boost::any value = queue.back();
      boost::packaged_task&lt;?&gt; task = boost::packaged_task&lt;?&gt;(boost::move(value));
      // actually execute task
      task();
      queue.pop_back();
  }
  </code></pre>
  
  <p>The '?' denote what I'm unsure about. I can't call executeSingle with a template, as it's called from a separate thread. I tried using boost::any, but I get the error:</p>
  
  <pre><code>  conversion from 'boost::any' to non-scalar type  boost::detail::thread_move_t&lt;boost:thread&gt;' requested.
  </code></pre>
  
  <p>The funny part is, I actually don't care about the return type of packaged_task at this point, I just want to execute it, but I can figure out the template details.</p>
  
  <p>Any insight would be greatly appreciated!</p>