Lambdas in C++ can capture variables from the enclosing scope to use within the lambda body. There are two primary ways to capture:
[var]
 or [=]
 to capture all local variables by value.[&var]
 or [&]
 to capture all local variables by reference.Here's an example demonstrating both:
#include <iostream>
int main() {
int a{1};
int b{2};
auto by_val = [a]() { return a; };
auto by_ref = [&b]() { b++; };
std::cout << by_val() << ' ' << a << '\n';
by_ref();
std::cout << b << '\n';
}
1 1
3
Capture by value is used when you want the lambda to have its own copy of the data that won't be modified externally. Capture by reference is used when you want to modify external data or avoid copying.
Some additional notes:
[a, &b]
, capturing some variables by value and others by reference.this
 in a class method will give access to class members.Answers to questions are automatically generated and may not have been reviewed.
Learn about first-class functions in C++: a feature that lets you store functions in variables, pass them to other functions, and return them, opening up new design possibilities