Static and thread-local objects shall be constant-initialized
Static and thread-local objects shall be constant-initialized.
Static and thread-local objects are initialized at the start of code execution. The C++ language standard only partially defines the initialization order of multiple static or thread-local objects and the order can change from build to build. If you initialize a static or thread-local object from another such object, the compiler might access the latter object before it is initialized. To avoid access before initialization, initialize static and thread-local objects by using objects that evaluate to a constant at compile time. Initialization with constants occurs before initialization with variables and often happens at compile time.
This rule applies to global variables, static variables, static class member variables, and static function-scope variables.
Polyspace® flags initializations of static or thread-local objects using initializers and constructors that do not evaluate to constants at compile time. To constant-initialize static or thread-local objects, use:
A constexpr constructor with only constant arguments
A constant expression
A value
Because string objects use dynamic memory allocation of unknown size, the compiler cannot evaluate them at compile time. Polyspace flags initialization of string objects irrespective of whether you specify an initializer.
If you expect a rule violation but do not see it, refer to the documentation for Polyspace Bug Finder™ or Polyspace Bug Finder Server™.
| Group: Basic concepts |
| Category: Required, Automated |