Skip to main content
GitHub

DCL59-CPP. Do not define an unnamed namespace in a header file

Unnamed namespaces are used to define a namespace that is unique to the translation unit, where the names contained within have internal linkage by default. The C++ Standard, [namespace.unnamed], paragraph 1 [ ISO/IEC 14882-2014 ], states the following:

An unnamed-namespace-definition behaves as if it were replaced by:

  inline namespace unique { /* empty body */ }
  using namespace unique ;
  namespace unique { namespace-body }

where inline appears if and only if it appears in the unnamed-namespace-definition , all occurrences of unique in a translation unit are replaced by the same identifier, and this identifier differs from all other identifiers in the entire program.

Production-quality C++ code frequently uses header files as a means to share code between translation units. A header file is any file that is inserted into a translation unit through an #include directive. Do not define an unnamed namespace in a header file. When an unnamed namespace is defined in a header file, it can lead to surprising results. Due to default internal linkage, each translation unit will define its own