An unnamed-namespace-definition behaves as if it were replaced by
inline
optnamespace
unique{ /* empty body */ }
using namespace unique ;
namespace unique { namespace-body }
where
inline
appears if and only if it appears in the unnamed-namespace-definition and all occurrences of unique in a translation unit are replaced by the same identifier, and this identifier differs from all other identifiers in the translation unit. The optional attribute-specifier-seq in the unnamed-namespace-definition appertains to unique.
I used the example given by @Johannes Schaub - litb in this answer in SO:
namespace A { void f(long); }
using namespace A;
namespace {
void f(int);
void g() {
::f(0);
}
}
If I replace the unnamed-namespace-definition according to [namespace-unnamed]/1, given above, I get the following code:
namespace A { void f(long); }
using namespace A;
namespace unique {}
using namespace unique;
namespace unique {
void f(int);
void g() {
::f(0);
}
}
int main(){
g();
}
which shows that the function g()
calls unique::f(int)
as can be seen here.
If I replace the unnamed-namespace-definition in the code by
namespace unique { namespace-body }
using namespace unique ;
I get the following code:
namespace A { void f(long); }
using namespace A;
namespace unique {
void f(int);
void g() {
::f(0);
}
}
using namespace unique;
int main(){
g();
}
which shows that the function g()
now calls A::f(long)
as can be seen here and confirms what @Johannes Schaub said on his answer. I have been trying to find an explanation for these two different behaviors by the compiler in the Standard, to no avail.