MISRA Discussion Forums
Conflict 12-1-3 and 12-1-2 - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: AUTOSAR C++:2014 rules (https://forum.misra.org.uk/forumdisplay.php?fid=185)
+--- Thread: Conflict 12-1-3 and 12-1-2 (/showthread.php?tid=1613)



Conflict 12-1-3 and 12-1-2 - Tobias Loose - 08-04-2022

Hi!

The text of A12-1-2:


Quote:Both NSDMI and a non-static member initializer in a constructor shall not be used in the same type.


And A12-1-3:

Quote: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.


These are in conflict if a type initializes some members to a constant cross all constructors, but other members to non-constant values. The conflict arises from having to move the constant member's initializer to NSDMI and having to do the same thing with the other members (for A12-1-2) but not being able to (due to their dependence on constructor arguments).

Initializing to one value and then overwriting in the ctor body with another value is not an option when the member is const or when there is no cheap first initialization (e.g. due to dynamically allocated memory etc.).

I suggest revising A12-1-3 to:

Quote:If all user-defined constructors of a class initialize all data members with constant values that are the same across all constructors, then data members shall be initialized using NSDMI instead.


Thanks!


RE: Conflict 12-1-3 and 12-1-2 - misra cpp - 13-05-2022

We agree that there is a conflict between A12-1-2 and A12-1-3, and your proposed solution would be one way of unifying them.

We plan to revisit these rules in the next version.