Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Stateful Lambdas | Working with Predicates and Algorithms
C++ Functional Utilities

bookStateful Lambdas

When you use lambdas in C++, you can capture variables from the surrounding scope. By default, lambdas only allow you to modify captured variables if they are captured by reference. In the previous example, the counter variable is captured by reference using [&counter], so each time the lambda is called, it increments the original counter variable, and you see the updated value printed.

If you capture a variable by value, the lambda receives its own copy of the variable. Normally, you cannot modify this copy inside the lambda unless you explicitly mark the lambda as mutable. Declaring a lambda as mutable allows you to change the values of variables captured by value inside the lambda body. However, these modifications affect only the lambda's copy, not the original variable outside the lambda.

Mutable lambdas are useful when you want to maintain some internal state across multiple lambda invocations, but you do not want to expose or modify the original variable in the outer scope. This approach is especially helpful in algorithms where each lambda call needs to track its own state independently, or when you want to avoid side effects on external variables.

Understanding when and how to use mutable lambdas can help you write more expressive and powerful functional code in C++, giving you better control over state within your algorithms.

main.cpp

main.cpp

copy
1234567891011121314151617
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers{1, 2, 3, 4, 5}; int counter = 0; std::for_each(numbers.begin(), numbers.end(), [&counter](int n) { ++counter; std::cout << "Element: " << n << ", Counter: " << counter << '\n'; }); std::cout << "Total elements processed: " << counter << '\n'; return 0; }
question mark

When would you need to declare a lambda as mutable in C++?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3

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 mutable lambda in C++?

When should I use mutable lambdas instead of capturing by reference?

What happens if I try to modify a captured-by-value variable without using mutable?

bookStateful Lambdas

Stryg for at vise menuen

When you use lambdas in C++, you can capture variables from the surrounding scope. By default, lambdas only allow you to modify captured variables if they are captured by reference. In the previous example, the counter variable is captured by reference using [&counter], so each time the lambda is called, it increments the original counter variable, and you see the updated value printed.

If you capture a variable by value, the lambda receives its own copy of the variable. Normally, you cannot modify this copy inside the lambda unless you explicitly mark the lambda as mutable. Declaring a lambda as mutable allows you to change the values of variables captured by value inside the lambda body. However, these modifications affect only the lambda's copy, not the original variable outside the lambda.

Mutable lambdas are useful when you want to maintain some internal state across multiple lambda invocations, but you do not want to expose or modify the original variable in the outer scope. This approach is especially helpful in algorithms where each lambda call needs to track its own state independently, or when you want to avoid side effects on external variables.

Understanding when and how to use mutable lambdas can help you write more expressive and powerful functional code in C++, giving you better control over state within your algorithms.

main.cpp

main.cpp

copy
1234567891011121314151617
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers{1, 2, 3, 4, 5}; int counter = 0; std::for_each(numbers.begin(), numbers.end(), [&counter](int n) { ++counter; std::cout << "Element: " << n << ", Counter: " << counter << '\n'; }); std::cout << "Total elements processed: " << counter << '\n'; return 0; }
question mark

When would you need to declare a lambda as mutable in C++?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3
some-alt