AUTOSAR C++14 Rule A12-1-3
If all user-defined constructors of a class initialize data members with constant values that are the same across all constructors, then data members shall be initialized using NSDMI instead
Since R2021b
Description
Rule Definition
If all user-defined constructors of a class initialize data members with constant values that are the same across all constructors, then data members shall be initialized using NSDMI instead.
Rationale
You might be using user-defined class constructors to initialize the nonstatic data members of your class. When all user-defined constructors initialize nonstatic data members to the same value, using a user-defined constructor for initialization purposes is unnecessary. The best practice is to initialize the nonstatic data members directly in the class definition. Such in-class nonstatic data member initialization (NSDMI) enables you to use the error-free and efficient implicit constructor to obtain an instance of the class that has the data members initialized to a default values.
Polyspace Implementation
Polyspace flags a nonstatic data member declaration if either of these conditions is true:
The nonstatic data member is not initialized in-class and all user-defined constructors initialize the data member to the same value.
The nonstatic data member is initialized in-class and at the same time, it is also initialized in user-defined constructors.
This checker does not apply to:
Copy and move constructors
Union definitions
Arrays that are initialized in constructors
Objects that are initialized field by field in constructors
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 |
Version History
Introduced in R2021b