AutosarC++17_10-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
Using global and static variables of a non-POD type makes the API of a class to bespurious about its true dependencies, as they can be accessed from any place of thesource code. Using static or global variables makes the code more difficult to maintain,less readable and significantly less testable.
Another problem is that the order in which constructors and initializers for staticvariables are called is only partially specified in the C++ Language Standard and caneven change from build to build. This can cause issues that are difficult to find or debug.
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 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#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.