Lambda expressions should not be defined inside another lambda expression
Lambda expressions should not be defined inside another lambda expression.
Developers can use lambda expressions to write anonymous function objects that contain a
few lines of code. Nesting lambda expression reduces the readability of the code because the
body of a lambda expression is typically in the line where it is used. For instance, the
find_if algorithm takes a unary predicate as one of its arguments. A
developer can use a lambda expression to define a predicate condition in the declaration of
find_if. In this code snippet, the find_if algorithm
returns the first member of a vector of integers that is greater than 2 and that is
even.
std::vector<int> v = { 1, 2, 3, 4 };
std::find(v.begin(), v.end(),
[](int val) { return val>2 && val%2==0;});Polyspace® flags lambda expressions that are defined inside another lambda expression. Polyspace also highlights the closest nesting lambda expression.
If you expect a rule violation but do not see it, refer to Coding Standard Violations Not Displayed.
| Group: Expressions |
| Category: Advisory, Automated |