AutosarC++19_03-A5.1.4¶
A lambda expression object shall not outlive any of its reference-captured objects
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
When an object is captured by reference in a lambda, lifetime of the object is not tied to the lifetime of the lambda.
If a lambda object leaves the scope of one of its reference-captured object, the execution of the lambda expression results in an undefined behavior once the reference-captured object is accessed.
Example
// $Id: A5-1-4.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <functional>
std::function<std::int32_t()> F()
{
std::int32_t i = 12;
return ([&i]() -> std::int32_t {
i = 100;
return i;
}); // Non-compliant
}
std::function<std::int32_t()> G()
{
std::int32_t i = 12;
return ([i]() mutable -> std::int32_t { return ++i; }); // Compliant
}
void Fn()
{
auto lambda1 = F();
std::int32_t i = lambda1(); // Undefined behavior
auto lambda2 = G();
i = lambda2(); // lambda2() returns 13
}
See Also
- SEI CERT C++ [10]: EXP61-CPP. A lambda object must not outlive any of its reference captured objects.
- C++ Core Guidelines [11]: F.53: Avoid capturing by reference in lambdas that will be used nonlocally, including returned, stored on the heap, or passed to another thread.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
lambda_outlives_object |
Lambda expression object outlives reference-captured object ‘{}’. |
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
allow_function_reference_parameter_capture¶
allow_function_reference_parameter_capture : bool = False
allow_longer_living_local¶
allow_longer_living_local : bool = False
consider_constructors_as_capturing¶
consider_constructors_as_capturing : bool = False