首页 > 解决方案 > Pass a lambda with a move capture

问题描述

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?

标签: c++

解决方案


推荐阅读