Namespaces introduce new declarative regions for declarations, reducing the likelihood of conflicting identifiers with other declarative regions. One feature of namespaces is that they can be further extended, even within separate translation units. For instance, the following declarations are well-formed.
The standard library introduces the namespace
std for standards-provided declarations such as
std::for_each. However, it is undefined behavior to introduce new declarations in namespace
std except under special circumstances. The C++ Standard, [namespace.std], paragraphs 1 and 2 [ISO/IEC 14882-2014], states the following:
1 The behavior of a C++ program is undefined if it adds declarations or definitions to namespace
stdor to a namespace within namespace
stdunless otherwise specified. A program may add a template specialization for any standard library template to namespace
stdonly if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.
2 The behavior of a C++ program is undefined if it declares
— an explicit specialization of any member function of a standard library class template, or
— an explicit specialization of any member function template of a standard library class or class template, or
— an explicit or partial specialization of any member class template of a standard library class or class template.
In addition to restricting extensions to the the namespace
std, the C++ Standard, [namespace.posix], paragraph 1, further states the following:
The behavior of a C++ program is undefined if it adds declarations or definitions to namespace
posixor to a namespace within namespace
posixunless otherwise specified. The namespace
posixis reserved for use by ISO/IEC 9945 and other POSIX standards.
Do not add declarations or definitions to the standard namespaces
posix, or to a namespace contained therein, except for a template specialization that depends on a user-defined type that meets the standard library requirements for the original template.
The Library Working Group, responsible for the wording of the Standard Library section of the C++ Standard, has an unresolved issue on the definition of user-defined type. Although the Library Working Group has no official stance on the definition [INCITS 2014], we define it to be any
enum that is not defined within namespace
std or a namespace contained within namespace
std. Effectively, it is a user-provided type instead of a standard library–provided type.
Noncompliant Code Example
In this noncompliant code example, the declaration of
x is added to the namespace
std, resulting in undefined behavior.
This compliant solution assumes the intention of the programmer was to place the declaration of
x into a namespace to prevent collisions with other global identifiers. Instead of placing the declaration into the namespace
std, the declaration is placed into a namespace without a reserved name.
Noncompliant Code Example
In this noncompliant code example, a template specialization of
std::plus is added to the namespace
std in an attempt to allow
std::plus to concatenate a
MyString object. However, because the template specialization is of a standard library–provided type (
std::string), this code results in undefined behavior.
The interface for
std::plus requires that both arguments to the function call operator and the return type are of the same type. Because the attempted specialization in the noncompliant code example results in undefined behavior, this compliant solution defines a new
std::binary_function derivative that can add a
std::string to a
MyString object without requiring modification of the namespace
In this compliant solution, a specialization of
std::plus is added to the
std namespace, but the specialization depends on a user-defined type and meets the Standard Template Library requirements for the original template, so it complies with this rule. However, because
MyString can be constructed from
std::string, this compliant solution involves invoking a converting constructor whereas the previous compliant solution does not.
Altering the standard namespace can cause undefined behavior in the C++ standard library.
|Axivion Bauhaus Suite|
|Do not modify the standard namespaces 'std' and 'posix'|
4032, 4035, 4631
|SonarQube C/C++ Plugin|
|[INCITS 2014]||Issue 2139, "What Is a User-Defined Type?"|
|[ISO/IEC 14882-2014]||Subclause 126.96.36.199.1, "Namespace |
Subclause 188.8.131.52.2, "Namespace