Variables shall not be implicitly captured in a lambda expression
Variables shall not be implicitly captured in a lambda expression.
In a lambda expression, you have the option to capture variables implicitly. For instance, this lambda expression
[&](std::int32_t var) {
sum+ = var;
}If a variable in the body of the expression comes from the calling context.
For instance, in the preceding lambda expression, it is not clear if
sum is captured from the calling context or is a global
variable.
If all variables captured from the calling context are used and whether the variables are modified or just read (If the variables are read, a by-copy capture is preferred).
If you capture variables explicitly in a lambda expression, you have more control on whether to capture by reference or copy. In addition, you or a reviewer can read the lambda expression and determine whether a variable was captured from the calling context.
If you expect a rule violation but do not see it, refer to Coding Standard Violations Not Displayed.
| Group: Expressions |
| Category: Required, Automated |