title: Pass Member Function to Variadic Template Function
body: <p>I have a class with a function called enqueue:</p>
  
  <pre><code>template&lt;class T, class... Args&gt;
  inline auto ThreadPool::enqueue(T &amp;&amp; t, Args&amp;&amp;... args) -&gt;std::future&lt;typename std::result_of&lt;T(Args ...)&gt;::type&gt;
  {
      using return_type = typename std::result_of&lt;T(Args...)&gt;::type;
  
      auto task = std::make_shared&lt;std::packaged_task&lt;return_type()&gt;&gt; (
         std::bind(std::forward&lt;T&gt;(t), std::forward&lt;Args&gt;(args)...)
      );
  
      std::future&lt;return_type&gt; result = task-&gt;get_future();
      {
          std::unique_lock&lt;std::mutex&gt; lock(m_mutex);
  
          // Don't allow job creation after stopping pool.
          if (m_done)
              throw std::runtime_error("Enqueue on stopped ThreadPool.");
  
          m_tasks.emplace([task]() { (*task)(); });
      }
  
      m_cond.notify_one();
      m_futures.push_back(move(result));
      return result;
  }
  </code></pre>
  
  <p>This is an inline implementation done inside of the header file alongside the ThreadPool class.</p>
  
  <p>It is a singleton and should be able to take any function with its arguments, add this to a task queue and return a future of the result type of that function.</p>
  
  <p>Here is the class where I am trying to use it:</p>
  
  <pre><code>void Grepper::scan(std::tr2::sys::path const&amp; folder, std::string expression, bool verbose) {
      // Create directory iterators.
      std::tr2::sys::recursive_directory_iterator d(folder);
      std::tr2::sys::recursive_directory_iterator e;
  
      // Create tasks from files that match initial extension list.
      for (; d != e; ++d) {
          if (!std::tr2::sys::is_directory(d-&gt;status()) &amp;&amp; std::find(m_extensions.begin(), m_extensions.end(), d-&gt;path().extension().generic_string()) != m_extensions.end()) {
              ThreadPool::get_instance().enqueue(grep, d-&gt;path(), expression, verbose);
          }
      }
  }
  </code></pre>
  
  <p>Which gives a compiler error of:</p>
  
  <pre><code>Error C3867 'Grepper::grep': non-standard syntax; use '&amp;' to create a pointer to member 
  </code></pre>
  
  <p>I have tried creating a functor to this function as well as passing the function as a lambda:</p>
  
  <pre><code>ThreadPool::get_instance().enqueue([this](std::tr2::sys::path p, std::string s, bool b) { grep(p, s, b); });
  </code></pre>
  
  <p>Which gives me the following compiler error:</p>
  
  <pre><code>Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &amp;&amp;,_Types &amp;&amp;...)'
  </code></pre>
  
  <p>For reference here is the declaration of my grep method:</p>
  
  <pre><code>void grep(std::tr2::sys::path file, std::string expression, bool verbose);
  </code></pre>
  
  <p>How do I pass this function and its arguments properly to the enqueue method?</p>