Main Content

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

expand all

#include <cstdint>
#include <string>
class MessageBox1
{
public:
	MessageBox1() : LowerLeft(0), UpperRight(0.0F), msg()
	{
	}
	// ...

private:

	int LowerLeft;     // Noncompliant
	float UpperRight;  // Noncompliant
	std::string msg;    // Noncompliant
};

class MessageBox2
{
public:
	// ...

private:
	int LowerLeft = 0;     // Compliant
	float UpperRight = 0.0F;         // Compliant
	std::string msg = "";   // Compliant
};
class MessageBox3
{
public:
	MessageBox3() : LowerLeft(0), UpperRight(0.0F), msg()
	{
	}
	

private:
	int LowerLeft = 0;      // Noncompliant 
	float UpperRight = 0.0F;          // Noncompliant
	std::string msg = "";    // Noncompliant 
};
class MessageBox4
{
public:
	MessageBox4() : LowerLeft(0), UpperRight(0.0F), msg()
	{
	}
	// ...
	MessageBox4(int int_i): LowerLeft(int_i),UpperRight(0.1F), msg("str"){}
private:

	int LowerLeft;     // Compliant
	float UpperRight;            // Compliant - Initialized differently in two c'tor
	std::string msg = "";//Noncompliant -  Initialized differently in two c'tor
};

In this example, Polyspace flags nonstatic data member initializations that violate this rule. For instance:

  • In class MessageBox1, the declarations of the data members are noncompliant because they are initialized by a user-defined constructor instead of by in-class initialization. The best practice is to declare such data members directly in-class and to use the default implicit constructors.

  • In class MessageBox2, the declarations of the data members are compliant because they are initialized directly in-class and the class defines no user-defined constructor.

  • In class MessageBox3, the declarations of the data members are noncompliant because they are initialized in-class while the constructor of the class also initialize the data members. The best practice is to declare the data members in class and omit a user defined constructor.

  • In class MessageBox4:

    • The declaration of AA::LowerLeft and AA::UpperLeft are compliant because two different constructors initialize them to different values and they are not initialized in-class.

    • The declaration of AA::msg is noncompliant because two different constructors initialize it to different value and it is also initialized in-class.

Check Information

Group: Special member functions
Category: Required, Automated

Version History

Introduced in R2021b