Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When moving a unique_ptr into a lambda, why is it not possible to call reset?

When moving std::unique_ptr into the lambda, it is not possible to call reset() on it, because it seems to be const then:

error C2662: void std::unique_ptr<int,std::default_delete<_Ty>>::reset(int *) noexcept': cannot convert 'this' pointer from 'const std::unique_ptr<int,std::default_delete<_Ty>>' to 'std::unique_ptr<int,std::default_delete<_Ty>> & 
#include <memory> int main() {     auto u = std::unique_ptr<int>();     auto l = [v = std::move(u)]{         v.reset(); // this doesn't compile     }; } 
  1. Why does this happen?
  2. Is it possible to capture the std::unique_ptr in another way which allows calling reset() within the lambda (with C++17 or later)?
like image 782
Roi Danton Avatar asked Jul 04 '19 15:07

Roi Danton


People also ask

What happens when you move a Unique_ptr?

A unique_ptr can only be moved. This means that the ownership of the memory resource is transferred to another unique_ptr and the original unique_ptr no longer owns it.

What does Unique_ptr Reset do?

std::unique_ptr::resetDestroys the object currently managed by the unique_ptr (if any) and takes ownership of p. If p is a null pointer (such as a default-initialized pointer), the unique_ptr becomes empty, managing no object after the call.

Can Unique_ptr be passed to function?

Because the unique pointer does not have a copy constructor. Hence you cannot pass it by value, because passing by value requires making a copy.


1 Answers

  1. Why does this happen?

Because the function-call operator of a lambda,

Unless the keyword mutable was 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 this operator().

and

  1. Is it possible to capture the std::unique_ptr in another way which allows to call reset() 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(); }; 
like image 153
songyuanyao Avatar answered Sep 21 '22 12:09

songyuanyao