Quick Q: std::move with std::shared_ptr in lambda

Save to:
Instapaper Pocket Readability

Quick A: Lambda captures are const by default, and it is not possible to move a const.

Recently on SO:

std::move with std::shared_ptr in lambda

The captured variable ptr in a lambda is by default const, i.e. the type of ptr is const std::shared_ptr<int>.

std::move cannot move out const objects, so a copy is created instead.

If you really want to move it, the ptr must be allowed to be mutable:

auto lambda = [ptr]() mutable {

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.