Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a sub-namespace with the name of another namespace

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading