AUTOSAR C++14 Rule A8-5-2
Braced-initialization {}, without equals sign, shall be used for variable initialization
Description
Rule Definition
Braced-initialization {}, without equals sign, shall be used for variable initialization.
Rationale
Braced initialization:
classType Object{arg1, arg2, ...};Prevents implicit narrowing conversions such as from
doubletofloat.Avoids the ambiguous syntax that leads to the problem of most vexing parse.
For instance, from the declaration:
It is not immediately clear ifResourceType aResource();
aResourceis a function returning a variable of typeResourceTypeor an object of typeResourceType.For more information, see
Ambiguous declaration syntax.
The rule also forbids the use of = sign for initialization because
the = sign can give the impression that an assignment or copy constructor
is invoked even in situations when it is not.
Polyspace Implementation
In general, the checker flags initializations of an object obj1 of
data type Type using these formats:
Type obj1 = obj2;
Type obj1(obj2);
The checker allows an exception for these cases:
Initialization of variables with type
autousing a simple assignment to a constant, a variable, a lambda expression, a standard initializer list or a function call.Initialization of reference types using a simple assignment
Declarations with global scope using the format
Type a()whereTypeis a class type with default constructor. The analysis interpretsaas a function returning the typeType.Loop variable initialization in OpenMP parallel
forloops, that is, inforloop statements that immediately follow#pragma omp parallel for
The checker is enabled only if you specify a C++ version of C++11 or later. See
C++ standard version (-cpp-version).
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
Check Information
| Group: Declarators |
| Category: Required, Automated |
Version History
Introduced in R2019a