我尝试了以下方法:
std::function<void ()> getAction(std::unique_ptr<MyClass> &&psomething){
//The caller given ownership of psomething
return [psomething](){
psomething->do_some_thing();
//psomething is expected to be released after this point
};
}
但它不编译.有什么想法吗?
But it does not compile. Any ideas?
更新:
建议,需要一些新语法来明确指定我们需要将所有权转移到 lambda,我现在正在考虑以下语法:
AS suggested, some new syntax is required to explicitly specify we need to transfer the ownership to the lambda, I am now thinking about the following syntax:
std::function<void ()> getAction(std::unique_ptr<MyClass> psomething){
//The caller given ownership of psomething
return [auto psomething=move(psomething)](){
psomething->do_some_thing();
//psomething is expected to be released after this point
};
}
它会是一个好的候选人吗?
Would it be a good candidate?
更新 1:
我将展示我对 move
和 copy
的实现如下:
I will show my implementation of move
and copy
as following:
template<typename T>
T copy(const T &t) {
return t;
}
//process lvalue references
template<typename T>
T move(T &t) {
return std::move(t);
}
class A{/*...*