AUTOSAR C++14 Rule A5-1-2
Variables shall not be implicitly captured in a lambda expression
Description
Rule Definition
Variables shall not be implicitly captured in a lambda expression.
Rationale
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.
Troubleshooting
If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Expressions |
Category: Required, Automated |
Version History
Introduced in R2019b