mercredi 30 décembre 2020

"Capture by move" does not prevent capture by reference

When I write [&,x](){ /* making use of x */ }, x is captured by value.

When I write [=,&x](){ /* making use of x */ }, x is captured by reference.

If I try writing [&x, x](){ … } I get an error telling me x can appear only once in a capture list.

However, [&x, y = std::move(x)](){ /* making use of x and y */ }(); compiles fine but I get a segmentation fault.

Why does the code even compile? Why does the standard allow that I capture a variable by reference and I also use it in a "capture by-move" init capture?

Aucun commentaire:

Enregistrer un commentaire