- Why does this happen?
Because the function-call operator of a lambda,
Unless the keyword
mutablewas used in the lambda-expression, the function-call operator is const-qualified and the objects that were captured by copy are non-modifiable from inside thisoperator().
and
- Is it possible to capture the
std::unique_ptrin another way which allows to callreset()within the lambda
You need to mark it mutable.
mutable: allows body to modify the parameters captured by copy, and to call their non-const member functions
e.g.
auto l = [v = std::move(u)]() mutable {
v.reset();
};