Letting the compiler determine the lambda type with auto
works just fine:
#include <memory>
#include <functional>
int main(void) {
std::unique_ptr<int> ptr(new int(1));
auto fn = [ capture = std::move(ptr) ] () {};
}
If the lambda type is explicitly defined, however, a compilation error occurs saying there was an attempt to call unique_ptr
's deleted copy constructor:
#include <memory>
#include <functional>
int main(void) {
std::unique_ptr<int> ptr(new int(1));
std::function<void()> fn = [ capture = std::move(ptr) ] () {};
}
Reduced output:
/media/hdd/home/vitor/Documents/parallel-tools/tests/reusable_thread.cpp:115:59: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]’
In file included from /usr/include/c++/7/memory:80:0,
from /media/hdd/home/vitor/Documents/parallel-tools/tests/reusable_thread.cpp:110:
/usr/include/c++/7/bits/unique_ptr.h:388:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
^~~~~~~~~~
What exactly is going on here?