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).
https://stackoverflow.com/questions/154469/unnamed-anonymous-namespaces-vs-static-functions
Unnamed namespace's still have the advantage of allowing you to define translation-unit-local types.

https://stackoverflow.com/questions/4422507/superiority-of-unnamed-namespace-over-static
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.

1
2
3
4
5
6
7
8
9
10
11
namespace Named {
    int stuff_named;
}

namespace {
    int stuff_unnamed;
}

int stuff_global;

static int stuff_global_static;



~$ clang++ -c -o test.o test.cc && nm -C test.o
0000000000000004 B stuff_global
0000000000000000 B Named::stuff_named
Last edited on
Topic archived. No new replies allowed.