In the example below, Y and X give a warning "variable has static storage duration and non-POD type" (pclint, Autosar A3-3-2).
struct Y {int t; Y() {t = 0;}};
class X {private: int t; public: X() {t = 0;}};
struct Z {int t;};
X x; // warning: variable 'x' has 'static' storage duration and non-POD type
Y y; // variable 'y' has 'static' storage duration and non-POD type
Z z;
I have 2 questions.
- What could happen before the constructor is called which justifies the warning?
Edit: In my case the global variable is only used in the standard namespace and data is accessed by global functions in this namespace. Therefore the constructor should be executed before data is accessed.
- How can I avoid the warning which occurs for Y and X? I want to avoid possible uninitialised state if used with automatic storage (as for Z), therefore I would like to keep the constructor or reach the goal somehow else.
One solution could be the use of a C++ wrapper class which would initialise the struct. Is there a simpler/alternative solution, where uninitialised use of member "int t" cannot occur?
From the AutoSAR C++14 Guidelines:
The general rationale for the rule is to avoid the complex rules of where dynamic initialization applies for static initialization, a common cause for e.g. the static initialization fiasco.
A simply fix for your examples is to make their constructors
constexpr, such that thestaticstorage duration objectsxandyare constant-initialized instead of zero-initialized as part of static initialization:E.g. (moving the initialization of the
tdata member by a constant expression to its default member initializer):or alternatively initializing the
tmember in the member initializer list of theconstexprconstructor:Finally, note that POD is no longer a standardese term as of C++11 (deprecated), and as the AutoSAR C++14 Guidelines naturally applies for C++14, one should consider entirely dropping the notion of POD class types; this likewise applies for pclint.