Capturing Variables
main.cpp
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.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
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?
Fantastiskt!
Completion betyg förbättrat till 4.55
Capturing Variables
Svep för att visa menyn
main.cpp
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.
Tack för dina kommentarer!