Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele 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.

main.cpp

main.cpp

copy
12345678910111213141516
#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'; }

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.

question mark

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

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 3

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookStateful Lambdas

Pyyhkäise näyttääksesi valikon

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.

main.cpp

main.cpp

copy
12345678910111213141516
#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'; }

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.

question mark

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

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 3
some-alt