According to the clause 3.5/4 of C++ Standard:
An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage.
Simultanously in paragraph 7.3.1.1 we have note 96):
Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit.
How to explicitly make external linkage for name inside unnamed namespace and how to check that linkage is actually external if Standard guaranteed that there is no way to access name defined inside unnamed namespace from another translation unit?
In which cases doing explicit external linkage for name inside unnamed namespace is useful?
The only way I can think of is to give it C language linkage, so that its linkage name ignores the namespace qualification:
(A strict reading of the standard suggests that
g
should have internal linkage, but that's not what compilers seem to do.)Typically ELF compilers will implement internal linkage with non-global symbols, so you can compile the code and inspect the object file:
The mangled name of the unnamed namespace can vary between compilers, but demangling it will show:
The lowercase
t
shows thatf
has local visibility, meaning it can't be linked to from other object files. The uppercaseT
shows thatg
has external linkage.This isn't guaranteed by the standard though, as ELF visibility is not part of the C++ standard, and some compilers implement linkage without using visibility even on ELF platforms, e.g. the EDG compiler produces a global symbol for the same code:
So using
extern "C"
allows you to give a name external linkage even if it appears in an unnamed namespace, but that doesn't make the note correct, because you can refer to that name from other translation units, because it doesn't use the unnamed namespace scope. That suggests to me that the note is simply a leftover from C++03 when entities in unnamed namespaces didn't automatically have internal linkage, and the note should be corrected or removed (and indeed T.C. points out it was already removed by DR 1603).I can't think of any cases where it's useful.