Adding a new member (even a whole namespace) to an already existing namespace alias

Viewed 61

Let library be a namespace from an external source (e.g. shared library). Let global be a global namespace for a project that uses that external source.

By using

namepace global
{
    namespace external = library;
}

the library namespace can now be used by accessing global::external. Now imaging other_library (with no conflicts with library) being a namespace from a second external source. Is it possible to add other_library to the alias external so that functionality from both external sources can be access via global::external?

Reason: provide a unified access to different resources by hiding underlying namespaces and grouping those into a different, custom API-friendly way.

1 Answers

This should do the trick

namespace global
{
    namespace external{
        using namespace library;
        using namespace other_library;
    }
}

Disclaimer: I am not 100% sure that unpacking namespaces with using is equivalent to namespace aliases especially w.r.t very dark corners of function overloading and ADL.

Related