3
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.

Maik Klein
  • 15,548
  • 27
  • 101
  • 197
  • 2
    "I think I can not move the lambda into functions because std::function requires the lambda to be copyable." -- [Right.](http://stackoverflow.com/q/7944635) "I think a work around would be to make a shared_ptr and copy it into my lambda, that should make the lambda copyable," -- [Right.](http://stackoverflow.com/q/20843271) "but I wonder if there is an alternative for std::function that works with lambdas that have to be moved?" -- [Nothing standard.](http://stackoverflow.com/q/25330716) –  Dec 26 '15 at 10:04

0 Answers0