Main Content

AUTOSAR C++14 Rule A5-1-3

Parameter list (possibly empty) shall be included in every lambda expression

Description

Rule Definition

Parameter list (possibly empty) shall be included in every lambda expression.

Rationale

You do not have to include a parameter list in a lambda expression. For instance, this expression is syntactically valid and indicates a closure that can be called without parameters:

[&counter] {
   ++counter;
}
However, without the (), you or a reviewer might not recognize this as a function object. It is visually clearer to use the parameter list (...) even when the list is empty. For instance:
[&counter]() {
   ++counter;
}

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

expand all

#include <cstdint>

void func() {
    std::int32_t count = 0;
    
    auto lambda1 = [&count] {++count;}; //Noncompliant
    auto lambda2 = [&count] () { //Compliant
        ++count;
    };
}

The lambda expression assigned to lambda1 does not have a parameter list and violates the rule. The issue is fixed when the same lambda expression is assigned to lambda2.

Check Information

Group: Expressions
Category: Required, Automated

Version History

Introduced in R2019b