Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Capturing by Reference vs by Value | Advanced Lambda Features
C++ Lambda Expressions

bookCapturing 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

main.cpp

copy
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; }
Note
Note

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.

question mark

What is the effect of capturing a variable by value in a C++ lambda?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 1

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

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

Awesome!

Completion rate improved to 12.5

bookCapturing by Reference vs by Value

Pyyhkäise näyttääksesi valikon

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

main.cpp

copy
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; }
Note
Note

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.

question mark

What is the effect of capturing a variable by value in a C++ lambda?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 1
some-alt