Martin877 Martin877 - 6 months ago 26
C++ Question

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

vector<int> vec;

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

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 */ });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download