Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Mutable Lambdas and Return Types | Advanced Lambda Features
C++ Lambda Expressions

bookMutable Lambdas and Return Types

Understanding how to make lambdas mutable and how to specify their return types opens up new possibilities for advanced C++ programming. By default, when you capture variables by value in a lambda, those variables are copied and cannot be modified inside the lambda body. However, sometimes you want to update a captured value within a lambda. This is where the mutable keyword comes into play. Additionally, C++ allows you to declare an explicit return type for a lambda, which is particularly useful when the return type is not straightforward or when you want to ensure a specific type is returned.

main.cpp

main.cpp

copy
1234567891011121314
#include <iostream> int main() { int counter = 0; auto increment = [counter]() mutable -> int { counter += 1; return counter; }; std::cout << increment() << std::endl; // Prints `1` std::cout << increment() << std::endl; // Prints `1` again std::cout << counter << std::endl; // Prints `0` }

In this example, the lambda captures counter by value. Using the mutable keyword allows the lambda to modify its own copy of counter, so counter += 1; is valid. However, these changes do not affect the original counter variable outside the lambda. Each time you call increment(), it operates on its own internal copy, which gets reset each invocation. Notice the explicit -> int after the parameter list. This declares that the lambda will return an int, which can be helpful if the return type is complex or if you want to avoid type deduction ambiguities. Specifying the return type can also make your code easier to read and more predictable, especially in more complex lambdas.

question mark

What does the mutable keyword enable in a lambda expression that captures variables by value, and how can you explicitly set the return type of a lambda in C++?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. 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

Awesome!

Completion rate improved to 12.5

bookMutable Lambdas and Return Types

Stryg for at vise menuen

Understanding how to make lambdas mutable and how to specify their return types opens up new possibilities for advanced C++ programming. By default, when you capture variables by value in a lambda, those variables are copied and cannot be modified inside the lambda body. However, sometimes you want to update a captured value within a lambda. This is where the mutable keyword comes into play. Additionally, C++ allows you to declare an explicit return type for a lambda, which is particularly useful when the return type is not straightforward or when you want to ensure a specific type is returned.

main.cpp

main.cpp

copy
1234567891011121314
#include <iostream> int main() { int counter = 0; auto increment = [counter]() mutable -> int { counter += 1; return counter; }; std::cout << increment() << std::endl; // Prints `1` std::cout << increment() << std::endl; // Prints `1` again std::cout << counter << std::endl; // Prints `0` }

In this example, the lambda captures counter by value. Using the mutable keyword allows the lambda to modify its own copy of counter, so counter += 1; is valid. However, these changes do not affect the original counter variable outside the lambda. Each time you call increment(), it operates on its own internal copy, which gets reset each invocation. Notice the explicit -> int after the parameter list. This declares that the lambda will return an int, which can be helpful if the return type is complex or if you want to avoid type deduction ambiguities. Specifying the return type can also make your code easier to read and more predictable, especially in more complex lambdas.

question mark

What does the mutable keyword enable in a lambda expression that captures variables by value, and how can you explicitly set the return type of a lambda in C++?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2
some-alt