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
double
tofloat
.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();
aResource
is a function returning a variable of typeResourceType
or 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
auto
using 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()
whereType
is a class type with default constructor. The analysis interpretsa
as a function returning the typeType
.Loop variable initialization in OpenMP parallel
for
loops, that is, infor
loop 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