vendredi 29 janvier 2016

Passing member functions with variadic template arguments to variadic constructors

I can't figure out how to pass member function with variadic template arguments to std::thread constructor. I have a method that receives function and its arguments and need to pass them to other method which is invoked in new thread and call passed function there. Here is simplified version:

class Test
{
public:
    template<typename Function, typename... Args>
    void Run(Function&& f, Args&&... args)
    {
        std::thread t(&Test::Operation, this, f, args...); // how???
        t.detach();
    }

    template<typename Function, typename... Args>
    void Operation(Function&& f, Args&&... args)
    {
        f(args...);
        // ...
    }
};

Test test;
test.Run([](const std::string& msg) { std::cout << msg; }, "Hi!");

There is something wrong in passing arguments this way, I get the following error: 'std::thread::thread': no overloaded function takes 4 arguments. How can I do this?

Aucun commentaire:

Enregistrer un commentaire