defining consts in class while adhering to misra with definition location

386 Views Asked by At

Generally speaking I would define a bunch of vars using constexpr in a class header file. This works functionally. In coming to MISRA compliance, when this class header is used in more than one file, it violates rule 3-2-2/3-2-4, which requires that a variable only be defined once (this is C++14, which from what I've found, can't be made inline, unless I make it a function).

How can I get around this? If I move them to the cpp file outside the class, then they are global scope and MISRA doesn't allow for global scope, so a namespace would need to be created just for the vars. I tried ways of using const, but that didn't work out, or maybe I was doing it wrong.

Is there something that the designers of MISRA were thinking that I am missing? It's obviously better to have a var in words with a description then some random number in the code.. so there must be some way to make this work correctly? Or does everyone just ignore this violation in this context?

1

There are 1 best solutions below

0
On

The intention of MISRA was that this rule should not apply to constants. Per their forum @ https://misra.org.uk/forum/viewtopic.php?f=188&t=1757

"This rule was not intended to apply to constants, and will be clarified for the next version"