The difference is the name of the mangled identifier (_ZN12_GLOBAL__N_11bE
vs _ZL1b
, which doesn't really matter, but both of them are assembled to local symbols in the symbol table (absence of .global
asm directive).
#include<iostream>
namespace {
int a = 3;
}
static int b = 4;
int c = 5;
int main (){
std::cout << a << b << c;
}
.data
.align 4
.type _ZN12_GLOBAL__N_11aE, @object
.size _ZN12_GLOBAL__N_11aE, 4
_ZN12_GLOBAL__N_11aE:
.long 3
.align 4
.type _ZL1b, @object
.size _ZL1b, 4
_ZL1b:
.long 4
.globl c
.align 4
.type c, @object
.size c, 4
c:
.long 5
.text
As for a nested anonymous namespace:
namespace {
namespace {
int a = 3;
}
}
.data
.align 4
.type _ZN12_GLOBAL__N_112_GLOBAL__N_11aE, @object
.size _ZN12_GLOBAL__N_112_GLOBAL__N_11aE, 4
_ZN12_GLOBAL__N_112_GLOBAL__N_11aE:
.long 3
All 1st level anonymous namespaces in the translation unit are combined with each other, All 2nd level nested anonymous namespaces in the translation unit are combined with each other
You can also have a nested namespace or nested inline namespace in an anonymous namespace
namespace {
namespace A {
int a = 3;
}
}
.data
.align 4
.type _ZN12_GLOBAL__N_11A1aE, @object
.size _ZN12_GLOBAL__N_11A1aE, 4
_ZN12_GLOBAL__N_11A1aE:
.long 3
which for the record demangles as:
.data
.align 4
.type (anonymous namespace)::A::a, @object
.size (anonymous namespace)::A::a, 4
(anonymous namespace)::A::a:
.long 3
//inline has the same output
You can also have anonymous inline namespaces, but as far as I can tell, inline
on an anonymous namespace has 0 effect
inline namespace {
inline namespace {
int a = 3;
}
}
_ZL1b
: _Z
means this is a mangled identifier. L
means it is a local symbol through static
. 1
is the length of the identifier b
and then the identifier b
_ZN12_GLOBAL__N_11aE
_Z
means this is a mangled identifier. N
means this is a namespace 12
is the length of the anonymous namespace name _GLOBAL__N_1
, then the anonymous namespace name _GLOBAL__N_1
, then 1
is the length of the identifier a
, a
is the identifier a
and E
closes the identifier that resides in a namespace.
_ZN12_GLOBAL__N_11A1aE
is the same as above except there's another namespace level in it 1A