A rule often depends on a local setting. A lambda's parameters receive values from its caller, while its capture list controls access to surrounding local variables. With [limit], the lambda stores a copy of limit when the lambda is created. That copied value is available in its body whenever the lambda is called.
This is capture by value. Changing the original local variable later does not update the stored copy. The capture therefore records the setting at creation time. By contrast, [&limit] borrows the original variable by reference, so its value can be observed as it changes, but the original must remain alive whenever the lambda uses it. This lesson uses value capture to avoid that lifetime dependency.
A lambda can be stored in an auto variable because the compiler knows its generated type. You can then call it like a function or pass it to an algorithm. Each element still arrives through the lambda parameter; the captured threshold is a separate part of the rule.
#include <iostream>
int main() {
int minimum = 10;
auto qualifies = [minimum](int value) { return value >= minimum; };
minimum = 20;
std::cout << qualifies(12) << '\n';
}
The example prints 1 because creating qualifies copied the earlier threshold of 10. In the starter, edit the existing affordable lambda in two places on the same line: its capture list and its return expression. Keep the declaration before limit = 25. Moving it below that assignment would capture the newer limit instead, even with the right capture syntax.
Here auto saved = [limit]() { return limit; }; is created while limit is 3. Changing the outside limit to 8 afterward leaves the captured copy unchanged, so saved() returns 3.
affordable with [limit] capture by value and return whether price <= limit. It should retain the original limit and print Affordable: 2 while the outer value prints Current limit: 25.Edit main.cpp, then press Run to inspect the Console. Press Submit to check your work. Keep the supplied probes in place.