Anonymous or no name namespace

Sep 23, 2022 at 1:49pm
Anyone outright understand what the necessary and purpose of bare, i.e. no name namespace ?
Sep 23, 2022 at 2:29pm
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.
Sep 23, 2022 at 2:34pm
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 Sep 23, 2022 at 2:39pm
Sep 23, 2022 at 7:28pm
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.
Sep 23, 2022 at 9:05pm
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 Sep 23, 2022 at 9:06pm
Topic archived. No new replies allowed.