c++c++11lambdac++14effective-c++

What would a default lambda capture mode via init capture be like?


With lambdas, in C++11, we can have a default capture mode set to by-value/by-ref, e.g. [=]/[&], optionally followed by explicit captures, by-ref/by-value, for some variables, e.g. [=,&this_is_by_ref] or [&,this_is_by_value].

In C++14, we can also have explicit captures by-move, e.g. [y = std::move(x)].

In Effective Modern C++, Item 32, 3rd paragraph, I read

The one thing you can't express with an init capture is a default capture mode, […]

What is the author most likely referring to?

We already have a way to capture all variables we need by copy or by reference. Why would we want to express that with the x = y form?

Maybe the author is referring only to the a "capture by move default"? Something which would work like [x = std::move(x), y = std::move(y), …] with all variables used in the body listed?


Solution

  • The answer by @OutOfBound is already good. I will only express a similar ideas from a different angle.

    Scott Meyers in the first few paragraphs of item 32 notices that:

    Notice also that the new capture syntax is so flexible that it allows not only for expressing your intention of using move semantics, but also for const references (Scott omits this latter point) and mutable lambdas.