开发者

How can identically named namespaces at different hierarchy levels be addressed without conflict?

开发者 https://www.devze.com 2023-02-20 22:32 出处:网络
(The problem I\'m solving involves a 3rd party lib that I cannot change) #include <list> //Third party lib namespace

(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.

0

精彩评论

暂无评论...
验证码 换一张
取 消