I\'m trying to create a object that can be given a function and its parameters to his constructor. This class will then call the given function inside a lambda that is inste
As said in the comments, this compile fine with gcc-4.9 (and above), but if you need to use gcc-4.8 you can add parameters to the lambda in the worker
constructor and pass the arguments via the std::thread
constructor:
class worker {
public:
template <class Fn, class... Args>
explicit worker(Fn f, Args ...args) {
t = std::thread([f](Args ...largs) -> void {
f(largs...);
}, std::move(args)...);
}
private:
std::thread t;
};
This will also create copy of the arguments in the lambda arguments, unlike the capture by reference you were using [&]
that was probably incorrect in this case (see @Yakk comment).