Home > Net >  Creating a sub-namespace with the name of another namespace
Creating a sub-namespace with the name of another namespace

Time:05-29

I want to do something like this:

namespace someModule
{
    int someInt = 4;
}

namespace test::someModule
{
    void f()
    {
        someModule::someInt = 2;
    }
}

But it looks like the compiler searches for someInt in test::someModule and not in someModule. I could create test as test::someModule_, but it looks pretty ugly. Isn't there a better option?

CodePudding user response:

you can always refer to the global namespace using :: as prefix, i.e.,

::someModule::someInt = 2;

Note that the fact you're having multiple identically named namespaces is probably pretty confusing! Unless you have a very good architectural reason that you explained why proposing to add such to a project I'm maintaining, I would reject this code – code like this is easy to misunderstand. In fact, it almost seems intentionally misleading! Your job as programmer is to write code that can be read and understood not only by machines, but also by yourself in a year or other developers tomorrow. When describing two different things (namespaces), use two different names. Using the same name is only fine if something does the same thing.

  • Related