Why does this code fail to compile?
#include <memory>
#include <utility>
int foo() {
auto num = std::make_unique<int>(1);
auto func = [s = std::move(num)] {
auto u = std::move(s); <-- ERROR!
return *u;
};
return func();
}
The error is:
<source>:8:14: error: call to deleted constructor of 'std::unique_ptr<int, std::default_delete<int>>'
auto u = std::move(s);
^ ~~~~~~~~~~~~
/opt/compiler-explorer/gcc-10.2.0/lib/gcc/x86_64-linux-gnu/10.2.0/../../../../include/c++/10.2.0/bits/unique_ptr.h:468:7: note: 'unique_ptr' has been explicitly marked deleted here
unique_ptr(const unique_ptr&) = delete;
I can't figure out why is the copy constructor being called, which is obviously deleted for a unique_ptr
by design. I can understand what the error is, but not why it's there in the first place.
Here's what I think happens but I'm not sure. If I unpack this lambda into a sort of struct
with the operator()
as
template <typename T>
struct Lambda{
:
:
operator() const{
auto u = std::move(s); // <-- error
}
private:
std::unique_ptr<T> s;
};
I think this would fail to compile because the move(s)
would change the value of s
which isn't allowed in a const
function. So compilations should've failed citing the immutability of the lamda. Even the fix to this error is by changing the lambda to be mutable
. But another one appears to be to make s
a shared_ptr
(which as per me should have failed since the lambda would still be immutable) and this is where I'm confused.
I've tried this on both clang
and gcc
with similar results. So, can please someone help with clearing the gap in my understanding?