1

Let's say I have a namespace:

namespace UI
{
}

And I have another namespace:

namespace Domain
{
}

Now let's say in the Domain namespace I also have a UI namespace that is specific for that domain.

namespace Domain
{
    namespace UI
    {
    }
}

Is it possible to import the global UI namespace into the domain UI namespace without importing it in the Domain namespace? I know using namespace is often not a good idea but conceptually the domain UI namespace should be both in the global UI namespace and in the domain namespace.

1 Answer 1

2

The only possible ways are namespace alias or use the entire namespace UI into Domain::UI. In both cases theres a way for access it from Domain namespace.

Examples:

namespace UI {
    class Test {

    };
}

namespace Domain {

}

namespace Domain {
    namespace UI {
        namespace _UI = ::UI;

        struct Test2 {
            _UI::Test param;
        };
    }
}

namespace Domain2 {

    namespace UI {
        using namespace ::UI;

        struct Test2 {
            Test param;
        };
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

yes, exactly, use ::UI when referencing to the global namespace.
Wow you can have namespace aliases didn't even know that. One more question if I do the 2nd solution the header, does this it import the global UI for all uses of the domain UI namespace?
No, only works in files including "using namespace ::UI" or files including a header with the using. Namespace in C++ not work like for example packages in Go. If you don't have a header defining something, theres no visibiltiy for that.
Using _UI as an identifier is undefined behavior. All identifiers beginning with an underscore and a capital letter are reserved for the implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.