How can identically named namespaces at different hierarchy levels be addressed without conflict?
(The problem I'm solving involves a 3rd party lib that I cannot change)
#include <list>
//Third party lib namespace
namespace foo
{
typedef int SomeType;
}
//my namespace
namespace mycompany
{
namespace groo
{
typedef std::list<foo::SomeType> SomeTypeList;
}
namespace foo
{
typedef std::list<foo::SomeType> SomeTypeList;
}
}
int main() { return 0; }
开发者_JAVA技巧
Attempting to compile this produces the error:
error: 'SomeType' is not a member of 'mycompany::foo'
Access from groo
works just fine. How do you access the shallower foo
from mycompany::foo
?
(I'll answer this myself, but figured I'd post the question in case someone else had the same)
When the compiler is confused about scope, you can always address a namespace absolutely. The global scope is ::
so foo::SomeType
's absolute scope name is ::foo::SomeType
I'm not really sure why the compiler doesn't automatically search the shallower namespace when it doesn't find the symbol in the deeper one though...
::foo::SomeType
ought to do it.
精彩评论