samedi 26 décembre 2015

Alternative for std::function that works with move lambdas?

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?

Aucun commentaire:

Enregistrer un commentaire