我想將捕獲 a 的可變 lambda 傳遞unique_ptr給另一個函式,如下面的代碼片段所示。
#include <functional>
#include <memory>
#include <iostream>
struct Adder {
Adder(int val1, std::unique_ptr<int> val2) : val1_(val1), val2_(std::move(val2)) {
}
int Add() {
return val1_ *val2_;
}
int val1_;
std::unique_ptr<int> val2_;
};
void CallAdder(std::function<int ()> func) {
std::cout << func() << "\n";
}
int main() {
auto ptr = std::make_unique<int>(40);
int byRef = 2;
auto lam = [&, p = std::move(ptr)]() mutable
{
return Adder(byRef, std::move(p)).Add();
};
CallAdder(std::move(lam));
}
上面的代碼為 lambda 提供了一個已洗掉的復制建構式錯誤,如下所示。
$ clang -14 -o lam lambda.cc
In file included from lambda.cc:2:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c /10/functional:59:
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c /10/bits/std_function.h:161:10: error: call to implicitly-deleted copy constructor of '(lambda at lambda.cc:25:16)'
new _Functor(*__source._M_access<const _Functor*>());
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c /10/bits/std_function.h:196:8: note: in instantiation of member function 'std::_Function_base::_Base_manager<(lambda at lambda.cc:25:16)>::_M_clone' requested here
_M_clone(__dest, __source, _Local_storage());
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c /10/bits/std_function.h:283:13: note: in instantiation of member function 'std::_Function_base::_Base_manager<(lambda at lambda.cc:25:16)>::_M_manager' requested here
_Base::_M_manager(__dest, __source, __op);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c /10/bits/std_function.h:611:33: note: in instantiation of member function 'std::_Function_handler<int (), (lambda at lambda.cc:25:16)>::_M_manager' requested here
_M_manager = &_My_handler::_M_manager;
^
lambda.cc:30:15: note: in instantiation of function template specialization 'std::function<int ()>::function<(lambda at lambda.cc:25:16), void, void>' requested here
CallAdder(std::move(lam));
^
lambda.cc:25:20: note: copy constructor of '' is implicitly deleted because field '' has a deleted copy constructor
auto lam = [&, p = std::move(ptr)]() mutable
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c /10/bits/unique_ptr.h:468:7: note: 'unique_ptr' has been explicitly marked deleted here
unique_ptr(const unique_ptr&) = delete;
^
我還嘗試將 lambda 作為參考傳遞,因為不能通過移動傳遞已洗掉的 lambda 函式?但是我遇到了一個型別錯誤,抱怨它無法std::function根據CallAdder簽名中的 對 lambda 進行型別檢查。我將如何修復此錯誤?
uj5u.com熱心網友回復:
CallAdder您可以在函式引數上模板。
[演示]
#include <functional>
#include <iostream>
#include <memory>
struct Adder {
Adder(int val1, std::unique_ptr<int> val2)
: val1_(val1), val2_(std::move(val2)) {}
int Add() { return val1_ *val2_; }
int val1_;
std::unique_ptr<int> val2_;
};
template <typename F>
void CallAdder(F&& func) { std::cout << std::forward<F>(func)() << "\n"; }
int main() {
auto ptr = std::make_unique<int>(40);
int byRef = 2;
auto lam = [&byRef, p = std::move(ptr)]() mutable {
return Adder(byRef, std::move(p)).Add();
};
CallAdder(std::move(lam));
}
// Outputs: 42
要不就:
[演示]
#include <functional>
#include <iostream>
#include <memory>
struct Adder {
Adder(int val1, std::unique_ptr<int> val2)
: val1_(val1), val2_(std::move(val2)) {}
int Add() { return val1_ *val2_; }
int val1_;
std::unique_ptr<int> val2_;
};
void CallAdder(std::function<int()> func) { std::cout << func() << "\n"; }
int main() {
auto ptr = std::make_unique<int>(40);
int byRef = 2;
auto lam = [&]() {
return Adder(byRef, std::move(ptr)).Add();
};
CallAdder(lam);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/534096.html
上一篇:使用分隔符陣列拆分文本
