Martin877 Martin877 - 26 days ago 6
C++ Question

What differences are between capturing by [&captured] and by [&local = captured] in lambdas?

vector<int> vec;

//a
auto foo = [&vec](){
//do something
};

//b
auto foo = [&v = vec](){
//do something
};


Do I understand it right that only difference between a and b is creation of alias "v" for "vec" in b case or is there more to it?

Answer Source

There is basically none. It's just there to allow you to give the captured value a custom name.

Note however that this syntax is required when you want to capture an rvalue reference:

std::vector<int> vector;
auto lambda = [vec = std::move(vector)]() { /* the lambda owns the vector now */ });