Anonymous or no name namespace

Anyone outright understand what the necessary and purpose of bare, i.e. no name namespace ?
There is no purpose in not having a namespace around things beyond not needing extra qualifiers or using statements.

Its pretty typical that your top level classes are in the global namespace (where they go if not in a defined one).

Otherwise, very small programs and older code may not use namespaces at all, or only lightly.
jonnin, abdul is specifically talking about the use of anonymous namespaces.

Somebody else can probably say more on this, but my understanding is that anonymous namespaces are a more flexible use of the C-style "static" variable/function declarations. An anonymous namespace allows you to have a "static" class (i.e. allows the same class name to be defined in multiple compilation units).
Unnamed namespace's still have the advantage of allowing you to define translation-unit-local types.
Last edited on
Ah, yes, I misread "i.e. no name namespace" to mean "I.E. no namespace".

The links probably cover it. I am not a fan; I prefer to go ahead an put a name on it, but its a tool you can use if you like the style. Putting a name on it means you need a using statement or qualifiers, but it also means you can extend outside the box later if you need to.
unnamed namespace is not an alternative to a named namespace. It's an alternative to using the keyword static to hide names from the linker.

namespace Named {
    int stuff_named;

namespace {
    int stuff_unnamed;

int stuff_global;

static int stuff_global_static;

~$ clang++ -c -o test.o && nm -C test.o
0000000000000004 B stuff_global
0000000000000000 B Named::stuff_named
Last edited on
Registered users can post here. Sign in or register to post.