为什么lambda init-capture对unique_ptr不起作用?
Why does lambda init-capture not work for unique_ptr?
我试图使用c++ 14初始捕获功能通过捕获将unique_ptr移动到lambda内。由于某种原因,gcc和clang都拒绝编译我的代码,坚持认为我试图复制一个显然不能工作的unique_ptr
。我认为避免复制正是init-capture + std::move
特性的要点——事实上,传递unique_ptr
似乎是每个人都使用的主要示例。
我做错了什么?
#include <functional>
#include <iostream>
#include <memory>
#include <string>
void runFunc(std::function<void()>&& f) {
auto ff = std::move(f);
ff();
}
int main() {
auto ptr = std::make_unique<std::string>("hello worldn");
runFunc([captured_ptr = std::move(ptr)]() {
std::cout << *captured_ptr;
});
}
gcc输出:
http://coliru.stacked-crooked.com/a/d91a480b2b6428acg++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
In file included from main.cpp:1:0:
/usr/local/include/c++/5.2.0/functional: In instantiation of 'static void std::_Function_base::_Base_manager<_Functor>::_M_clone(std::_Any_data&, const std::_Any_data&, std::false_type) [with _Functor = main()::<lambda()>; std::false_type = std::integral_constant<bool, false>]':
/usr/local/include/c++/5.2.0/functional:1746:16: required from 'static bool std::_Function_base::_Base_manager<_Functor>::_M_manager(std::_Any_data&, const std::_Any_data&, std::_Manager_operation) [with _Functor = main()::<lambda()>]'
/usr/local/include/c++/5.2.0/functional:2260:19: required from 'std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = main()::<lambda()>; <template-parameter-2-2> = void; _Res = void; _ArgTypes = {}]'
main.cpp:15:6: required from here
/usr/local/include/c++/5.2.0/functional:1710:34: error: use of deleted function 'main()::<lambda()>::<lambda>(const main()::<lambda()>&)'
__dest._M_access<_Functor*>() =
^
main.cpp:13:43: note: 'main()::<lambda()>::<lambda>(const main()::<lambda()>&)' is implicitly deleted because the default definition would be ill-formed:
runFunc([captured_ptr = std::move(ptr)]() {
^
main.cpp:13:43: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = std::__cxx11::basic_string<char>; _Dp = std::default_delete<std::__cxx11::basic_string<char> >]'
In file included from /usr/local/include/c++/5.2.0/memory:81:0,
from main.cpp:3:
/usr/local/include/c++/5.2.0/bits/unique_ptr.h:356:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
clang:
输出http://coliru.stacked-crooked.com/a/4374988d875fcedcIn file included from main.cpp:1:
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../include/c++/5.2.0/functional:1711:10: error: call to implicitly-deleted copy constructor of '(lambda at main.cpp:13:13)'
new _Functor(*__source._M_access<_Functor*>());
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../include/c++/5.2.0/functional:1746:8: note: in instantiation of member function 'std::_Function_base::_Base_manager<(lambda at main.cpp:13:13)>::_M_clone' requested here
_M_clone(__dest, __source, _Local_storage());
^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../include/c++/5.2.0/functional:2260:33: note: in instantiation of member function 'std::_Function_base::_Base_manager<(lambda at main.cpp:13:13)>::_M_manager' requested here
_M_manager = &_My_handler::_M_manager;
^
main.cpp:13:13: note: in instantiation of function template specialization 'std::function<void ()>::function<(lambda at main.cpp:13:13), void>' requested here
runFunc([captured_ptr = std::move(ptr)]() {
^
main.cpp:13:14: note: copy constructor of '' is implicitly deleted because field '' has a deleted copy constructor
runFunc([captured_ptr = std::move(ptr)]() {
^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../include/c++/5.2.0/bits/unique_ptr.h:356:7: note: 'unique_ptr' has been explicitly marked deleted here
unique_ptr(const unique_ptr&) = delete;
^
1 error generated.
因为std::function
必须是可复制的:
std::function
满足CopyConstructible和CopyAssignable的要求。
构造函数:
用
f
的副本初始化目标。
您正在构造的lambda(完全有效)具有unique_ptr
成员,这使得它不可复制。如果您将runFunc
重写为按值取任意函子:
template <typename F>
void runFunc(F f) {
auto ff = std::move(f);
ff();
}
可以编译。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- 由于 RPTR->num=30 与 (*rptr).num=30 相同,但是*((*rptr).ptr).name 不起作用为什么?