Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Capturing Variables | Functional Foundations in C++
C++ Functional Utilities

bookCapturing Variables

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526
#include <iostream> #include <vector> int main() { int value = 10; int reference = 20; // Lambda capturing 'value' by value and 'reference' by reference auto lambda = [value, &reference]() { std::cout << "Captured by value: " << value << std::endl; std::cout << "Captured by reference: " << reference << std::endl; }; // Modify the original variables value = 15; reference = 25; lambda(); std::cout << "After lambda call:" << std::endl; std::cout << "Original value: " << value << std::endl; std::cout << "Original reference: " << reference << std::endl; return 0; }

Lambdas in C++ allow you to capture variables from their enclosing scope, making them powerful tools for functional-style programming. The capture list, which appears in square brackets before the lambda's parameter list, determines which variables are captured and how they are captured.

You can capture variables by value or by reference. Capturing by value copies the variable's value into the lambda when it is created. This means that changes to the original variable after the lambda's creation do not affect the value inside the lambda. On the other hand, capturing by reference allows the lambda to refer directly to the original variable, so any changes to the variable are reflected when the lambda is executed.

The syntax for the capture list is straightforward:

  • Use the variable name alone to capture by value;
  • Use an ampersand (&) before the variable name to capture by reference;
  • Use [=] to capture all automatic variables by value;
  • Use [&] to capture all automatic variables by reference.

Understanding how capture lists work is essential for writing correct and predictable lambda expressions, especially when dealing with mutable state or side effects. Choosing between value and reference capture depends on whether you want your lambda to work with a snapshot of the variable's value or with its current state at the time of invocation.

question mark

What is the main difference between capturing a variable by value and by reference in a lambda's capture list?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you give an example of a lambda capturing variables by value and by reference?

What happens if I modify a captured variable inside the lambda?

When should I use value capture versus reference capture in practice?

bookCapturing Variables

Stryg for at vise menuen

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526
#include <iostream> #include <vector> int main() { int value = 10; int reference = 20; // Lambda capturing 'value' by value and 'reference' by reference auto lambda = [value, &reference]() { std::cout << "Captured by value: " << value << std::endl; std::cout << "Captured by reference: " << reference << std::endl; }; // Modify the original variables value = 15; reference = 25; lambda(); std::cout << "After lambda call:" << std::endl; std::cout << "Original value: " << value << std::endl; std::cout << "Original reference: " << reference << std::endl; return 0; }

Lambdas in C++ allow you to capture variables from their enclosing scope, making them powerful tools for functional-style programming. The capture list, which appears in square brackets before the lambda's parameter list, determines which variables are captured and how they are captured.

You can capture variables by value or by reference. Capturing by value copies the variable's value into the lambda when it is created. This means that changes to the original variable after the lambda's creation do not affect the value inside the lambda. On the other hand, capturing by reference allows the lambda to refer directly to the original variable, so any changes to the variable are reflected when the lambda is executed.

The syntax for the capture list is straightforward:

  • Use the variable name alone to capture by value;
  • Use an ampersand (&) before the variable name to capture by reference;
  • Use [=] to capture all automatic variables by value;
  • Use [&] to capture all automatic variables by reference.

Understanding how capture lists work is essential for writing correct and predictable lambda expressions, especially when dealing with mutable state or side effects. Choosing between value and reference capture depends on whether you want your lambda to work with a snapshot of the variable's value or with its current state at the time of invocation.

question mark

What is the main difference between capturing a variable by value and by reference in a lambda's capture list?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2
some-alt