This is a draft version of a MISRA C++ 202x rule proposed for public review.
MISRA Rule 10.3.1
Category: Advisory
Analysis Type: Decidable,Single Translation Unit
Rationale
An unnamed namespace is unique within each translation unit. Any declarations [1] appearing in an unnamed namespace within a
header file [2] refer to different entities in each translation unit, which might not be consistent with developer
expectations.
Example
// Header.hpp
namespace // Non-compliant
{
inline int32_t x;
}
void fn_a();
// File1.cpp
#include "Header.hpp"
void fn_a()
{
x = 42;
}
// File2.cpp
#include "Header.hpp"
void fn_b()
{
fn_a(); // Assigns 42 to 'x' in translation unit for 'File1.cpp'
if ( x == 42 ) {} // 'x' within this translation unit will not have the value 42
}
Glossary
[1] Declaration
A declaration introduces the name of an entity into a translation unit (see [basic.def]/1).
An entity may be declared several times. The first declaration of an entity in a translation unit is
called an introduction [3]. All subsequent declarations are called redeclarations [4].
A definition [5] is a declaration, as described in [basic.def]/2.
[2] Header file
A header file is considered to be any file that is included during preprocessing (for example via the #include
directive),
regardless of its name or suffix.
[3] Introduction
See declaration [1].
[4] Redeclaration
See declaration [1].
[5] Definition
See declaration [1].
Copyright The MISRA Consortium Limited © 2023