The following code does not compile:
struct S{};
void foo(std::unique_ptr<S> ptr)
{
auto l = [p = std::move(ptr)]()
{
auto p2 = std::move(p);
};
l();
}
The reason is that std::move(p)
returns an lvalue reference and hence the compiler tries to call the copy constructor, which is deleted. Why does move
return an lvalue reference here?
Here is a full sample.
& ptr)` to pass (and move) the unique ptr into the lambda. Alternately, the move could be at the calling function.– doug Aug 18 '19 at 17:53