0

I have a lambda function with a move capture that I want to pass to another function.

void DoSomething(std::function<void()> && func) {
    //do something with the function
}

auto ptr = std::make_unique<object>();
auto func = [ptr = std::move(ptr)](){
    // do something
};

DoSomething(std::move(func)); // compiler error

This causes a compiler error because std::function tries to access the copy constructor which is deleted by the capture of the unique_ptr. Any better way of accomplishing this?

samuelnj
  • 1,627
  • 1
  • 10
  • 19
  • If you have control over `DoSomething`, just make it a template like `template void DoSomething(Func&& func) { use func here }` – NathanOliver Jul 30 '19 at 20:29
  • @NathanOliver I like that, but it doesn't quite work in my code since ultimately the use of `std::function` was to store the lambda functions with `move` captures into a `std` container. Ended up converting to a `std::shared_ptr` to make the lambda copy-able but wanted to see if there was a way of avoiding that.. – samuelnj Jul 31 '19 at 15:30

0 Answers0