我正在以这种方式捕获lambda表达式中的unique_ptr:
auto str = make_unique<string>("my string");
auto lambda = [ capturedStr = std::move(str) ] {
cout << *capturedStr.get() << endl;
};
lambda();
在我尝试将capturedStr
移动到另一个unique_ptr之前,它很有用。例如,以下内容无效:
auto str = make_unique<string>("my string");
auto lambda = [ capturedStr = std::move(str) ] {
cout << *capturedStr.get() << endl;
auto str2 = std::move(capturedStr); // <--- Not working, why?
};
lambda();
以下是编译器的输出:
.../test/main.cpp:11:14: error: call to implicitly-deleted copy
constructor of 'std::__1::unique_ptr<std::__1::basic_string<char>,
std::__1::default_delete<std::__1::basic_string<char> > >'
auto str2 = std::move(capturedStr);
^ ~~~~~~~~~~~~~~~~~~~~~~ ../include/c++/v1/memory:2510:31: note: copy constructor is implicitly
deleted because 'unique_ptr<std::__1::basic_string<char>,
std::__1::default_delete<std::__1::basic_string<char> > >' has a
user-declared move constructor
_LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
^ 1 error generated.
为什么不能移动capturedStr
?
答案 0 :(得分:50)
默认情况下,lambda的operator ()
为const
,您无法从const
对象移动。
如果要修改捕获的变量,请将其声明为mutable
。
auto lambda = [ capturedStr = std::move(str) ] () mutable {
// ^^^^^^^^^^
cout << *capturedStr.get() << endl;
auto str2 = std::move(capturedStr);
};
答案 1 :(得分:7)
auto lambda = [ capturedStr = std::move(str) ] {
cout << *capturedStr.get() << endl;
auto str2 = std::move(capturedStr); // <--- Not working, why?
};
为了提供更多细节,编译器正在有效地进行这种转换:
class NameUpToCompiler
{
unique_ptr<string> capturedStr; // initialized from move assignment in lambda capture expression
void operator()() const
{
cout << *capturedStr.get() << endl;
auto str2 = std::move(capturedStr); // move will alter member 'captureStr' but can't because of const member function.
}
}
在lambda上使用mutable将从operator()成员函数中删除const,从而允许更改成员。
答案 2 :(得分:5)
更明确地提出建议:添加mutable
:http://coliru.stacked-crooked.com/a/a19897451b82cbbb
#include <memory>
int main()
{
std::unique_ptr<int> pi(new int(42));
auto ll = [ capturedInt = std::move(pi) ] () mutable { };
}