Capturing by Reference vs by Value
When using a lambda, you can capture variables from the surrounding scope either by value (makes a copy) or by reference (uses the original). Capturing by value keeps the outer variable unchanged, while capturing by reference allows the lambda to modify it.
main.cpp
1234567891011121314151617181920212223242526#include <iostream> int main() { int number = 10; // Lambda capturing by value auto byValue = [number]() mutable { number += 5; std::cout << "Inside byValue lambda: " << number << '\n'; }; // Lambda capturing by reference auto byReference = [&number]() { number += 5; std::cout << "Inside byReference lambda: " << number << '\n'; }; byValue(); std::cout << "After byValue lambda, number: " << number << '\n'; byReference(); std::cout << "After byReference lambda, number: " << number << '\n'; return 0; }
Choose capture mode carefully — it determines whether changes inside the lambda affect variables outside it.
In the example above, the output from the lambda that captures by value shows that changes to number inside the lambda do not affect the original variable. The value of number outside the lambda remains unchanged after the lambda runs. In contrast, the lambda that captures by reference modifies the actual number variable, so changes inside the lambda are visible outside as well.
Use value capture when you want to protect the original variable from modification, or when you want to ensure the lambda operates on a snapshot of the value. Use reference capture when you need the lambda to modify variables from the enclosing scope or keep them in sync with changes made inside the lambda.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Awesome!
Completion rate improved to 12.5
Capturing by Reference vs by Value
Desliza para mostrar el menú
When using a lambda, you can capture variables from the surrounding scope either by value (makes a copy) or by reference (uses the original). Capturing by value keeps the outer variable unchanged, while capturing by reference allows the lambda to modify it.
main.cpp
1234567891011121314151617181920212223242526#include <iostream> int main() { int number = 10; // Lambda capturing by value auto byValue = [number]() mutable { number += 5; std::cout << "Inside byValue lambda: " << number << '\n'; }; // Lambda capturing by reference auto byReference = [&number]() { number += 5; std::cout << "Inside byReference lambda: " << number << '\n'; }; byValue(); std::cout << "After byValue lambda, number: " << number << '\n'; byReference(); std::cout << "After byReference lambda, number: " << number << '\n'; return 0; }
Choose capture mode carefully — it determines whether changes inside the lambda affect variables outside it.
In the example above, the output from the lambda that captures by value shows that changes to number inside the lambda do not affect the original variable. The value of number outside the lambda remains unchanged after the lambda runs. In contrast, the lambda that captures by reference modifies the actual number variable, so changes inside the lambda are visible outside as well.
Use value capture when you want to protect the original variable from modification, or when you want to ensure the lambda operates on a snapshot of the value. Use reference capture when you need the lambda to modify variables from the enclosing scope or keep them in sync with changes made inside the lambda.
¡Gracias por tus comentarios!