mardi 6 janvier 2015

c++14 and call to a function with a lambda capturing a unique_ptr

I am trying to pass a lambda expression (with a unique_ptr captured) to another function, but it is not working:


Here is my lambda:



auto str = make_unique<string>("my string");
auto lambda = [ capturedStr = std::move(str) ]() mutable {
auto str2 = std::move(capturedStr);
cout << *str2.get() << endl;
};


And here is my function:



void myTest(std::function<void()> f)
{
f();
}


When I try to call myTest(lambda); I get the following error:



.../test/main.cpp:19:8: error: call to implicitly-deleted copy constructor of '' myTest(lambda); ^~~~~~ .../test/main.cpp:14:17: note: copy constructor of '' is implicitly deleted because field '' has a deleted copy constructor auto lambda = capturedStr = std::move(str) mutable { ^ ../include/c++/v1/memory:2510:31: note: copy constructor is implicitly deleted because 'unique_ptr, std::__1::default_delete > >' has a user-declared move constructor _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT ^ ../include/c++/v1/functional:1452:19: note: passing argument to parameter here function(_Fp, typename enable_if ^ 1 error generated.



Any idea why?


Aucun commentaire:

Enregistrer un commentaire