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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 12.5

bookMutable Lambdas and Return Types

Desliza para mostrar el menú

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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2
some-alt