Capturing Local Variables in C++ Lambdas

How can I capture local variables from the enclosing scope when using lambdas in C++? What are the different capture modes and when should I use each?

Lambdas in C++ can capture variables from the enclosing scope to use within the lambda body. There are two primary ways to capture:

  1. Capture by value - Capture a copy of the variable. Denoted by [var] or [=] to capture all local variables by value.
  2. Capture by reference - Capture a reference to the variable. Denoted by [&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:

  • Capturing by reference can lead to dangling references if the referenced data goes out of scope, so extra care must be taken.
  • You can mix captures, like [a, &b], capturing some variables by value and others by reference.
  • Capturing this in a class method will give access to class members.
  • Avoid capturing by reference if the lambda will outlive the current scope.

First Class Functions

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

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Returning Function Pointers from Functions
How can I return a function pointer from a function in C++? What are some use cases for doing this?
Downcasting Function Pointers in C++
Is it possible to downcast a function pointer to a more specific function pointer type in C++? For example, casting a pointer-to-base to a pointer-to-derived. If so, how is it done and what are the risks?
Function Pointers and Virtual Functions
How do function pointers interact with virtual functions in C++? Can I take a pointer to a virtual function?
Understanding Complex Function Pointer Syntax
C++ function pointer syntax can get quite complex, especially with things like typedef and using directives. How can I decipher complex function pointer syntax?
Function Pointers and Static Member Functions
Can I take a function pointer to a static member function in C++? How is it different from taking a pointer to a non-static member function?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant