An unnamed namespace will be unique within each translation unit. Any declarations appearing in an unnamed namespace in a header will refer to a different entity in each translation unit, which is probably not the expected behavior.


== Noncompliant Code Example

----
// Header.hpp
namespace                  // Noncompliant
{
  extern int32_t x;
}
----

----
// File1.cpp
#include "Header.hpp"

namespace
{
  int32_t x;
}

void fn_a(void)
{
  x = 42;
}
----

----
// File2.cpp
#include "Header.hpp"

namespace
{
  int32_t x;  // this is a different x than in File1.cpp
}

void fn_b(void)
{
  fn_a();                  // Is expected to initialize "x" to 42
  if (x == 42)             // But does not, as there are 2 distinct "x" variables
  {
  }
}
----


== See

* MISRA {cpp}:2008, 7-3-3 - There shall be no unnamed namespaces in header files.
* https://wiki.sei.cmu.edu/confluence/x/VXs-BQ[CERT, DCL59-CPP.] - Do not define an unnamed namespace in a header file


ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)

include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]
