AutosarC++17_03-A5.1.2¶
Variables shall not be implicitly captured in a lambda expression
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Capturing variables explicitly helps document the intention of the author. It also allows for different variables to be explicitly captured by copy or by reference within the lambda definition.
Exceptions
It is allowed to implicitly capture variables with non-automatic storage duration.
Example
// $Id: A5-1-2.cpp 271752 2017-03-23 12:07:07Z piotr.tanski $
#include <algorithm>
#include <cstdint>
#include <vector>
void fn1(const std::vector<std::int32_t> &v) {
std::uint64_t sum = 0;
std::for_each(v.begin(), v.end(),
[&](std::int32_t lhs) { sum += lhs; }); // Non-compliant
sum = 0;
std::for_each(v.begin(), v.end(),
[&sum](std::int32_t lhs) { sum += lhs; }); // Compliant
}
void fn2() {
constexpr std::uint8_t n = 10;
static std::int32_t j = 0;
[n]() {
std::int32_t array[n]; // Compliant
j += 1; // Compliant by exception
};
}
See Also
- HIC++ v4.0 [8]: 5.1.4 Do not capture variables implicitly in a lambda.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
implicit_lambda_capture |
Variables shall not be implicitly captured in a lambda expression. |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
immediately_invoking_function_names¶
immediately_invoking_function_names : set[bauhaus.analysis.config.QualifiedName] = {'std::find_if', 'std::find_if_not', 'std::invoke'}
report_non_transient_lambda¶
report_non_transient_lambda : bool = False
report_non_transient_lambda_capturing_this¶
report_non_transient_lambda_capturing_this : bool = False
this to non-transient lambda
functions.