AUTOSAR C++14 Rule A12-1-2
Both NSDMI and a non-static member initializer in a constructor shall not be used in the same type
Since R2020b
Description
Rule Definition
Both NSDMI and a non-static member initializer in a constructor shall not be used in the same type.
Rationale
You can initialize a non-static data member of a class in one of these ways:
In the declaration of the member in the class body by using the non-static data member initializer (NSDMI)
By using a non-static member initializer in a constructor
In a class, initializing a subset of the non-static data members by using the NSDMI and initializing the remaining non-static data members by using a constructor reduces code readability. This code pattern might cause confusion for the reader about which initial values for each data member are actually used. Using either the NSDMI or a constructor to initialize all non-static data members of the class avoids this potential confusion.
The move and copy constructors are exempt from this rule because these constructors have the special behavior of initializing the data members by using their existing values from other objects. These constructors are unlikely to cause the confusion for the reader.
Polyspace Implementation
If you use the NSDMI to initialize a subset of the non-static data members of a class and a constructor to initialize the remaining non-static data members, the checker flags the constructor and the associated NSDMI initializations.
The checker does not flag the move and copy constructors that violate this rule.
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: Special member functions |
Category: Required, Automated |