I'm trying to move a lambda that has move-captured a unique_ptr, which fails for a reason I don't understand. To my understanding, a lambda expression that has a movable member should be movable.
I'm using clang 3.8.1-24 to compile the following minimal example code.
auto p = std::make_unique<int>(42);
auto l = [p{std::move(p)}]{};
std::vector<std::function<void()>> v;
v.push_back(std::move(l));
The compiler complains about the push_back:
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/functional:1571:10: error: call to implicitly-deleted copy constructor of '(lambda at test.cpp:8:11)'
new _Functor(*__source._M_access<_Functor*>());
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/functional:1606:8: note: in instantiation of member function 'std::_Function_base::_Base_manager<(lambda at test.cpp:8:11)>::_M_clone' requested here
_M_clone(__dest, __source, _Local_storage());
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/functional:2116:33: note: in instantiation of member function 'std::_Function_base::_Base_manager<(lambda at test.cpp:8:11)>::_M_manager' requested here
_M_manager = &_My_handler::_M_manager;
^
test.cpp:10:14: note: in instantiation of function template specialization 'std::function<void ()>::function<(lambda at test.cpp:8:11), void, void>' requested here
v.push_back(std::move(l));
^
test.cpp:8:12: note: copy constructor of '' is implicitly deleted because field '' has a deleted copy constructor
auto l = [p{std::move(p)}]{};
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/unique_ptr.h:359:7: note: 'unique_ptr' has been explicitly marked deleted here
unique_ptr(const unique_ptr&) = delete;
^
Removing the move-capture makes the code compile. I would greatly appreciate any insights as to why this is happening and if the error is on my side or that of the standard library.
Any workarounds would also be very helpful, as I need to move this lambda into a container.