mercredi 30 décembre 2020

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?

Aucun commentaire:

Enregistrer un commentaire