Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Stateful Lambdas | Working with Predicates and Algorithms
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 3

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

bookStateful Lambdas

Glissez pour afficher le menu

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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 3
some-alt