Rule A2-10-4 (required, implementation, automated)
The identifier name of a non-member object with static storage duration or static function shall not be reused within a namespace.
Rationale
No identifier with static storage duration should be re-used in the same namespace across any source files in the project. This may lead to the developer or development tool confusing the identifier with another one.
Example
//% $Id: A2-10-4.cpp 305382 2018-01-26 06:32:15Z michal.szczepankiewicz $
#include <cstdint>
// f1.cpp
namespace ns1
{
static std::int32_t globalvariable = 0;
}
// f2.cpp
namespace ns1
{
// static std::int32_t globalvariable = 0; // Non-compliant - identifier reused
// in ns1 namespace in f1.cpp
}
namespace ns2
{
static std::int32_t globalvariable =
0; // Compliant - identifier reused, but in another namespace
}
// f3.cpp
static std::int32_t globalvariable =
0; // Compliant - identifier reused, but in another namespace
See also
MISRA C++ 2008 [7]: advisory 2-10-5 The identifier name of a non-member object or function with static storage duration should not be reused. Rule A2-10-5 (advisory, design / implementation, automated) Anidentifier name of a function with static storage duration or a non-member object with external or internal linkage should not be reused.
Rationale
Regardless of scope, no identifier with static storage duration should be re-used across any source files in the project. This includes objects or functions with external linkage and any objects or functions with static storage class specifier. While the compiler can understand this, the possibility exists for the developer or development tool to incorrectly associate unrelated variables with the same name. Note: This rule does not apply to objects without linkage, e.g. function local static objects.
Example
//% $Id: A2-10-5.cpp 305382 2018-01-26 06:32:15Z michal.szczepankiewicz $
#include <cstdint>
// f1.cpp
namespace n_s1
{
static std::int32_t globalvariable = 0;
}
static std::int32_t filevariable = 5; // Compliant - identifier not reused
static void Globalfunction();
// f2.cpp
namespace n_s1
{
// static std::int32_t globalvariable = 0; // Non-compliant - identifier reused
static std::int16_t modulevariable = 10; // Compliant - identifier not reused
}
namespace n_s2
{
static std::int16_t modulevariable2 = 20;
}
static void Globalfunction();
// Non-compliant - identifier reused
static std::int16_t modulevariable2 = 15; // Non-compliant - identifier reused
See also
MISRA C++ 2008 [7]: advisory 2-10-5 The identifier name of a non-member object or function with static storage duration should not be reused.
Rule A2-11-1 (required, design / implementation, automated)Volatile keyword shall not be used.
Rationale
The volatile keyword disables compiler optimizations for a particular variable or object’s value in case those values may change in ways not specified by the language (e.g. object representing a hardware register). It is error prone and often misused by developers, as they expect this is equal to variable or object’s value being atomic.
Note: The main intention of this rule is to eliminate incorrect usages of volatile keyword and force developers to precisely document each usage of volatile keyword.
## See also
JSF December 2005 [8]: AV Rule 205: The volatile keyword shall not be used
unless directly interfacing with hardware.
HIC++ v4.0 [9]: 18.2.3: Do not share volatile data between threads.
C++ Core Guidelines [11]: CP.8: Don’t try to use volatile for synchronization.
C++ Core Guidelines [11]: CP.200: Use volatile only to talk to non-C++ memory.
Rule A2-13-1 (required, architecture / design / implementation,automated) Only those escape sequences that are defined in ISO/IEC
## Rationale
The use of an undefined escape sequence leads to undefined behavior. The defined
escape sequences (ISO/IEC 14882:2014) are: \’, \", \?, \\, \a, \b, \f, \n, \r, \t, \v, \<Octal
Number>, \x<Hexadecimal Number>.
Note: Universal-character-names (\u hex-quad and \U hex-quad hex-quad) are also
allowed in character and string literals (although they look similar to escape
sequences, they are handled in a different way by the C++ language, see A2-13-6).
## Example
```cpp
//% $Id: A2-13-1.cpp 305629 2018-01-29 13:29:25Z piotr.serwa $
#include <string>
void F()
{
const std::string
a = "\k";
// Non-compliant
const std::string
b = "\n";
// Compliant
const std::string
c = "\U0001f34c";
// Compliant
}
See also
MISRA C++ 2008 [7]: required 2-13-1 Only those escape sequences that are defined in ISO/IEC14882:2003 shall be used. Rule A2-13-6 (required, architecture / design / implementation,automated) Universal character names shall be used only inside character or string literals.
Rationale
Using universal-character-names to define a language identifier can be confusing for a developer and may be troublesome to use this identifier in the source code.
Example
//% $Id: A2-13-6.cpp 307578 2018-02-14 14:46:20Z michal.szczepankiewicz $
#include <string>
void F()
{
const std::string c = "\U0001f34c"; // Compliant
}
//non-compliant
void \U0001f615()
{
//
}