c++ - 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?
解决方案
推荐阅读
- security - Firefox 无法建立连接 - websocket 和 heroku
- ruby - 如何使用 Gosu 库制作悬停效果
- ios - 如何在 iOS Swift4.2 中验证名称(John D'Largy)?
- extjs - 如何使用来自渲染器功能的记录作为编辑器 ExtJs Grid 内组合的存储
- c# - 重定向(url)不重定向
- ios - iOS - RxRealm - 即使数据集没有更改,是否可以获得更新的事件
- python-3.x - Python3:为什么在重新分配新对象变量后调用 __del__?
- java - ews-java-api 创建新约会
- objective-c - 为什么后台使用backgroundSession上传任务时会打印进度回调信息
- java - 使用 micronaut cli 创建控制器时如何选择包/文件夹?