It is legal in C++ to declare nested anonymous namespaces in the following manner:
namespace {
namespace {
struct Foo
{
};
}
}
namespace {
struct Foo // SAME IDENTIFIER AS <unnamed>::<unnamed>::Foo!!!
{
};
}
However, how would you declare an identifier using an explicitly typed Foo to avoid ambiguity?
EDITED — for all of you who do not read the question.
p.s. I have no intentions to use this sort of constructs, but I need to understand whether it is possible to disambiguate Foo in case someone finds a legitimate use for it. My compiler extension needs to handle all cases.
The C++ standard states it pretty clearly in 7.3.1.1p1:
So from above, we know that your code actually translates to the following:
Therefore, your firstI was wrong. Corrected by comments below.Foocan only be accessed withinnamespace unique1and the secondFoocan be accessed in the global namespace due tousing namespace unique3;.Then from 7.3.4p4:
Therefore, when you refer to
Foo, it can mean eitherunique1::unique2::Fooorunique3::Foo, which is an error. Note that the other answer says:has hidden unique name that cannot be accessed. This is incorrect, they can be accessed due to the using directives, it is just that both names are visible.However, by prepending the scope resolution operator
::toFooyou can accessunique3::Foobecause of the following:From 3.4.3.2p2:
The emphasized part says
using-directives in X, which in your case meansusing namespace unique1;andusing namespace unique3;, so the namespace-qualified lookup set looks like this:S(unique3::Foo), which meansunique1::unique2::Foois not included in the set and therefore is not an error.