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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 2

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you show an example of a mutable lambda in C++?

How does capturing by reference differ from capturing by value in lambdas?

When should I specify an explicit return type for a lambda?

Awesome!

Completion rate improved to 12.5

bookMutable Lambdas and Return Types

Veeg om het menu te tonen

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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 2
some-alt