AutosarC++17_03-A3.3.2¶
Non-POD type objects with static storage duration shall not be used
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
In general, using non-const global and static variables obscures the true dependencies of an API, since they can be accessed from any place of the source code. It therefore makes the code more difficult to maintain, less readable, and significantly less testable.
A particular problem is that the order in which constructors and initializers for static variables are called is only partially specified by the C++ Language Standard and can even change from build to build. This can cause issues that are difficult to find or debug.
The compiler performs constant-initialization, if
- the object is initialized by a constexpr constructor with only constant expression as arguments; or
- the object is not initialized by a constructor call, but is value-initialized (T object{};); or
- the object is not initialized by a constructor call, but is initialized by an initializer consisting only of constant expressions.
Constant initialization is guaranteed to occur before any other initialization of static or thread-local objects and may happen at compile time. Thus it is guaranteed that problematic dependencies between the initializers of constant-initialized static or thread-local objects cannot occur.
Note that declaring a static variable as constexpr (static is implied in this case, but may be added to the declaration), enforces constant initialization by the compiler.
Note that the rule applies to:
- global variables (i.e. extern)
- static variables
- static class member variables
- static function-scope variables
Exceptions
Defining “static constexpr” variables of non-POD type is permitted.
Example
// $Id: A3-3-2.cpp 271927 2017-03-24 12:01:35Z piotr.tanski $
#include <cstdint>
#include <limits>
#include <string>
class A // Non-POD type
{
public:
static std::uint8_t instanceId; // Compliant - static variable of POD type
static float const pi; // Compliant - static variable of POD type
static std::string const
separator; // Non-compliant - static variable of nonPOD type
// Implementation
};
std::uint8_t A::instanceId = 0;
float const A::pi = 3.14159265359;
std::string const A::separator = "==========";
class B {
public:
// Implementation
private:
static A a; // Non-compliant - static variable of non-POD type
};
class C {
public:
constexpr C() = default;
};
namespace {
constexpr std::int32_t maxInt32 =
std::numeric_limits<std::int32_t>::max(); // Compliant - static constexpr
// variable of POD type
A instance{}; // Non-compliant - static variable of non-POD type
constexpr C
c{}; // Compliant by exception - constexpr static variable of non-POD type
} // namespace
void fn() noexcept {
static A a{}; // Non-compliant
static std::int32_t counter{0}; // Compliant
}
class D // Singleton
{
public:
D() = default;
D(D const &) = default;
D(D &&) = default;
D &operator=(D const &) = default;
D &operator=(D &&) = default;
~D() = default;
private:
static B *instance; // Compliant - static variable of non-POD type, because
// it is a raw pointer
};
B *D::instance = nullptr;
See Also
- HIC++ v4.0 [8]: 3.3.1 Do not use variables with static storage duration.
- Google C++ Style Guide [11]: Static and Global Variables.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
static_nonpod_variable |
Do not use non-POD type objects with static storage duration. |
None |
False |
Options
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.