Advertisement
Advanced C++, Namespaces

How do namespaces interact with the one-definition rule and linkage?

Short answer

A fully-qualified name is unique across the program, so the same name in different namespaces are distinct entities with separate linkage—avoiding ODR violations that identical global names would cause.

Save C++ mock interview
Advertisement
AModel answer
A fully-qualified name is unique across the program, so the same name in different namespaces are distinct entities with separate linkage—avoiding ODR violations that identical global names would cause. Anonymous namespaces give internal linkage (per translation unit), while named-namespace entities generally have external linkage unless otherwise specified.
Real-world example
Placing two same-named helper types in different namespaces avoids an ODR clash at link time.
Likely follow-ups
  • How do namespaces avoid ODR clashes? | What linkage do anonymous namespaces give?
Related topics