What is an anonymous (unnamed) namespace?
Short answer
An unnamed namespace gives its contents internal linkage—they're visible only within the current translation unit, like static at file scope but working for types too.
Advertisement
AModel answer
An unnamed namespace gives its contents internal linkage—they're visible only within the current translation unit, like static at file scope but working for types too. It's the modern C++ way to make helper functions, constants, or types local to a .cpp file, avoiding linker collisions with same-named symbols elsewhere.
namespace {
int helper() { return 42; } // internal linkage
}Real-world example
File-local helpers go in an anonymous namespace so they don't clash with helpers in other files.
Likely follow-ups
- What linkage does it give? | How does it compare to file-scope static?
Related topics