Very little benefit from the global point of view: if viewed from other translation units' point of view, both approaches have same results: the anonymous namespace is invisible (or can't be referenced).
From the same translation unit point of view, there's a difference: The fact that you're defining a toplevel namespace means that you reduce the probability of importing a namespace conflict declared elsewhere, and the most common one would be for the global namespace (namespaceless functions, think of anything inherited from ISO C like from stdio.h or whatever).
For instance, if a global header you import in that translation unit had a "namespaceless" abort() and you declare a namespace { abort() { ...} } in your translation unit, you would have an ambiguity, gcc for instance would throw a compile error:
error: call of overloaded ‘abort()’ is ambiguous
Now, if you name an anonymous namespace inside a named namespace, you have these effects:
a) there's no ambiguity for functions declared inside the namespace, because it has precedence:
namespace a { namespace { abort() {...} } }
if you have a function like a::whatever() and it references abort(), it'll resolve at it's own namespace as it has precedence.
b) You won't have global linkage for a::abort() as it doesn't exist outside the translation unit, the same as namespace { abort(); } in the toplevel but without the potential conflict above.
And in "b" lies the difference: it's not the same as just namespace a { abort(); } because it won't have global linkage, so you could redefine it in another translation unit without conflicts. Good luck trying to link two translation units that both define namespace a { abort() { ... } } ...
So you get exactly as you mean:
namespace a { // you have a named space, so you don't have conflicts with the nameless one
namespace { // but you have local visibility and linkage
whatever(); // for this
}
}
To put in short: both ways have similarities but there's a difference. One could argue that's not very useful, but as a style it'll preemptively avoid collisions with the global namespace. One can still argue that since these would be caught at compile time (hopefully, at least when signatures matches perfectly), why bother. But it's a useful concept if your project is a library meant to be portable and your headers could get polluted depending on whatever the environment headers themselves imports, for otherwise your users would have to patch your library for their systems or you'd need #ifdefs here and there.
I program a lot on ISO/ANSI C 99 and from times to times I have to do stuff like:
#include <headerA.h>
#define symbol symbolB
#include <headerB.h>
// or some crap alike. And I have linker problems with above.
... because both headers (from e.g. different libraries) manage to pollute the namespace and I can't simply patch someone else's library.
C++ namespace resolves that, except when somebody else don't use it, so you must take measures either to prevent (which isn't an option for legacy code) or to counteract it.
X
so I don't think it makes a difference. If there were other namespaces ormain
I can understand it although to be honest in that case, I'd argue to rearrange the code translation units a bit. Thanks. – Holliholliday