Why does the following compile?
vector<int> vec;
auto lambda = [ vec (move(vec)) ]() { //??
};
How can I re-initialize an already assigned vec variable with vec (move(vec))
? Doesn't this call the move constructor?
If I write:
vector<int> vec;
vec (move(vec));
this is not valid
This is called an init-capture. It declaration of a new variable that shadows the above vec
. It's used to capture move-only types in lambda expressions:
An init-capture behaves as if it declares and explicitly captures a variable of the form “
auto init-capture ;
” whose declarative region is the lambda-expression’s compound-statement, [..]
More info on cppreference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With