int
main()
{
std::vector<std::function<void()>> functions;
auto t1 = std::make_unique<std::packaged_task<int()>>([] {
std::cout << "hello";
return 1 + 1;
});
auto fut = t1->get_future();
auto wrapped_func = [f = std::move(t1)] { (*f)(); };
// err
functions.push_back(std::move(wrapped_func));
}
I think I can not move the lambda into functions
because std::function
requires the lambda to be copyable.
I think a work around would be to make a shared_ptr
and copy it into my lambda, that should make the lambda copyable, but I wonder if there is an alternative for std::function
that works with lambdas that have to be moved?
Edit:
I also tried to create a move function myself
template <class Signature>
class move_func;
template <class R, class... Args>
class move_func<R(Args...)> : public std::function<R(Args...)>
{
move_func(const move_func&) = delete;
void operator=(const move_func&) = delete;
};
But I don't think it is enough just to delete the copy constructor and copy assignment operator.