Understanding lambda capture scope in C++
This is an easy C++ language-knowledge question that tests your grasp of how lambdas capture variables from their enclosing scope. It appears frequently in coding interviews because lambda capture rules are a common source of bugs—and a quick way to separate candidates who have internalized the rules from those who are guessing.
To work through this, you need to distinguish between capture by value, capture by reference, and the interaction between local and global scope when a lambda is defined. Pay close attention to whether variables are captured at lambda definition time or referenced at call time, and what the actual values or references point to when the lambda executes.
- Capture by value vs. capture by reference
- Default capture modes (
[=] and [&]) - Scope resolution and variable shadowing