AutosarC++17_03-A5.1.7

The underlying type of lambda expression shall not be used

Required inputs: IR

Rationale

“The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type [...]” [C++14 Language Standard] [3] Each lambda expression has a different unique underlying type, and therefore the type is not to be used either as function argument, template argument or operand to built-in or overloaded operator.

Example
// $Id: A5-1-7.cpp 271687 2017-03-23 08:57:35Z piotr.tanski $
#include <cstdint>
#include <functional>
#include <vector>
void fn() {
auto lambda1 = []() -> std::int8_t { return 1; };
auto lambda2 = []() -> std::int8_t { return 1; };

if (typeid(lambda1) == typeid(lambda2)) // Non-compliant - types of lambda1
                                  // and lambda2 are different
{
// ...
}

std::vector<decltype(lambda1)> v; // Non-compliant
// v.push_back([]() { return 1; }); // Compilation error, type of pushed
// lambda is different than decltype(lambda1)
// using mylambda_t = decltype([](){ return 1; }); // Non-compliant -
// compilation error
auto lambda3 = []() { return 2; };
using lambda3_t = decltype(lambda3); // Non-compliant - lambda3_t type can
                               // not be used for lambda expression
                               // declarations
// lambda3_t lambda4 = []() { return 2; }; // Conversion error at
// compile-time
std::function<std::int32_t()> f1 = []() { return 3; };
std::function<std::int32_t()> f2 = []() { return 3; };

if (typeid(f1) == typeid(f2)) // Compliant - types are equal
{
// ...
}
}
See Also
  • none

Possible Messages

Key

Text

Severity

Disabled

underlying_lambda_type

The underlying type of lambda expression shall not be used.

None

False

Options