How to pass variadic args to a std::thread?
<p>I would like to use my own Thread implementation by wrapping the std::thread class from C++11 so I will be able handle exceptions like I want.</p>

<p>Here is my wrap class:</p>

<pre><code>#include &lt;Types.hpp&gt;
#include &lt;thread&gt;
#include &lt;exception&gt;
#include &lt;functional&gt;

class Thread
{
    private:

        std::exception_ptr exceptionPtr;
        std::thread thread;

    public:

        using Id = std::thread::id;

        using NativeHandleType = std::thread::native_handle_type;

        Thread() noexcept = default;
        Thread(Thread &amp;&amp;t) noexcept :
            exceptionPtr(std::move(t.exceptionPtr)),
            thread(std::move(t.thread))
        {
        }

        Thread &amp;operator =(Thread &amp;&amp;t) noexcept
        {
            exceptionPtr = std::move(t.exceptionPtr);
            thread = std::move(t.thread);
            return *this;
        }

        template&lt;typename Callable, typename... Args&gt;
        Thread(Callable &amp;&amp;f, Args &amp;&amp;... args) :
            exceptionPtr(nullptr),
            thread([&amp;](Callable &amp;&amp;f, Args &amp;&amp;... args)
            {
                try
                {
                    std::once_flag flag;
                    std::call_once(flag, f, args...);
                }
                catch (...)
                {
                    exceptionPtr = std::current_exception();
                }

            }, f, args...)
        {
            if (exceptionPtr != nullptr)
            {
                 std::rethrow_exception(exceptionPtr);
            }
        }

        bool joinable() const noexcept
        {
            return thread.joinable();
        }

        void join()
        {
            thread.join();
        }

        void detach()
        {
            thread.detach();
        }

        Id getId() const noexcept
        {
            return thread.get_id();
        }

        NativeHandleType nativeHandle()
        {
            return thread.native_handle();
        }

        static uint32_t hardwareConcurrency() noexcept
        {
            return std::thread::hardware_concurrency();
        }

        static void wait(Time t)
        {
            std::this_thread::sleep_for(t);
        }
};
</code></pre>

<p>It works pretty well if there is no argument:</p>

<pre><code>Thread([&amp;]() {  /* do something */ }).detach();
</code></pre>

<p>... but if I try to pass variadic arguments:</p>

<pre><code>Thread(&amp;GUI::refreshTask, this, refreshDelay).detach();
</code></pre>

<p>... I get an error at compile time:</p>

<blockquote>
  <p>buildroot-2014.02/output/host/usr/i586-buildroot-linux-uclibc/include/c++/4.8.2/functional: In instantiation of 'struct std::_Bind_simple)(std::chrono::duration >); Args = {CRH::GUI const, std::chrono::duration >&amp;}]::__lambda1(void (CRH::GUI::<em>)(std::chrono::duration >), CRH::GUI</em>, std::chrono::duration >)>':
  buildroot-2014.02/output/host/usr/i586-buildroot-linux-uclibc/include/c++/4.8.2/thread:137:47:   required from 'std::thread::thread(_Callable&amp;&amp;, _Args&amp;&amp; ...) [with _Callable = CRH::Thread::Thread(Callable&amp;&amp;, Args&amp;&amp; ...) [with Callable = void (CRH::GUI::<em>)(std::chrono::duration >); Args = {CRH::GUI</em> const, std::chrono::duration >&amp;}]::__lambda1; _Args = {void (CRH::GUI::<em>&amp;)(std::chrono::duration >), CRH::GUI</em> const&amp;, std::chrono::duration >&amp;}]'
  /home/cyril/Documents/crh-2016/src/robot2/../core/Thread.hpp:72:30:   required from 'CRH::Thread::Thread(Callable&amp;&amp;, Args&amp;&amp; ...) [with Callable = void (CRH::GUI::<em>)(std::chrono::duration >); Args = {CRH::GUI</em> const, std::chrono::duration >&amp;}]'
  src/core/GUI.cpp:90:57:   required from here
  buildroot-2014.02/output/host/usr/i586-buildroot-linux-uclibc/include/c++/4.8.2/functional:1697:61: error: no type named 'type' in 'class std::result_of)(std::chrono::duration >); Args = {CRH::GUI const, std::chrono::duration >&amp;}]::__lambda1(void (CRH::GUI::<em>)(std::chrono::duration >), CRH::GUI</em>, std::chrono::duration >)>'
         typedef typename result_of&lt;_Callable(_Args...)>::type result_type;
                                                               ^
  buildroot-2014.02/output/host/usr/i586-buildroot-linux-uclibc/include/c++/4.8.2/functional:1727:9: error: no type named 'type' in 'class std::result_of)(std::chrono::duration >); Args = {CRH::GUI const, std::chrono::duration >&amp;}]::__lambda1(void (CRH::GUI::<em>)(std::chrono::duration >), CRH::GUI</em>, std::chrono::duration >)>'
           _M_invoke(_Index_tuple&lt;_Indices...>)</p>
</blockquote>

<p>It could be a little clearer... but it will be too demanding for GCC.</p>

<p>Any idea how to fix this issue?</p>

<p><strong>Solution</strong></p>

<pre><code>#include &lt;Types.hpp&gt;
#include &lt;thread&gt;
#include &lt;exception&gt;
#include &lt;functional&gt;

class Thread
{
    private:

        std::exception_ptr exceptionPtr;
        std::thread thread;

    public:

        using Id = std::thread::id;

        using NativeHandleType = std::thread::native_handle_type;

        Thread() noexcept = default;
        Thread(Thread &amp;&amp;t) noexcept :
            exceptionPtr(std::move(t.exceptionPtr)),
            thread(std::move(t.thread))
        {
        }

        Thread &amp;operator =(Thread &amp;&amp;t) noexcept
        {
            exceptionPtr = std::move(t.exceptionPtr);
            thread = std::move(t.thread);
            return *this;
        }

        template&lt;typename Callable, typename... Args&gt;
        Thread(Callable &amp;&amp;f, Args &amp;&amp;... args) :
            exceptionPtr(nullptr),
            thread([&amp;](typename std::decay&lt;Callable&gt;::type &amp;&amp;f, typename std::decay&lt;Args&gt;::type &amp;&amp;... args)
            {
                try
                {
                    std::bind(f, args...)();
                }
                catch (...)
                {
                    exceptionPtr = std::current_exception();
                }

            }, std::forward&lt;Callable&gt;(f), std::forward&lt;Args&gt;(args)...)
        {
        }

        bool joinable() const noexcept
        {
            return thread.joinable();
        }

        void join()
        {
            thread.join();

            if (exceptionPtr != nullptr)
            {
                std::rethrow_exception(exceptionPtr);
            }
        }

        void detach()
        {
            thread.detach();
        }

        Id getId() const noexcept
        {
            return thread.get_id();
        }

        NativeHandleType nativeHandle()
        {
            return thread.native_handle();
        }

        static uint32_t hardwareConcurrency() noexcept
        {
            return std::thread::hardware_concurrency();
        }

        static void wait(Time t)
        {
            std::this_thread::sleep_for(t);
        }
};
</code></pre>