developer
developer

Reputation: 13

Correct way to using namespace in nested namespace scope

We have in code one common namespace MainNamespace and a lot of namespace per module eg. ModuleNamespace, DifferentModuleNamespace. Module namespaces are inside the main namespace.

When we create a new class and need another class from different module we have to declare some using to avoid writing full namespace path.

What is consider as a good practice in such situation:

Using namespace with full path:

namespace MainNamespace {
namespace ModuleNamespace {

using MainNamespace::DifferentModuleNamespace::Foo;

       class MyClass {
           void method(Foo);
       };
}
}

or remove MainNamespace namespace from path:

namespace MainNamespace {
namespace ModuleNamespace {

using DifferentModuleNamespace::Foo;

   class MyClass {
       void method(Foo);
   };
}
}

or maybe different approach is better?

Edit:

Ok, maybe different question. Is there any situation when using absolute paths to namespace (using A = Main::ModuleA::A;) will be safer than using short paths(using A = ModuleA::C;). When we do it in the same main namespace.

file A.h:

namespace Main 
{
    namespace ModuleA {
        class A
        {
        public:
            A();
            ~A();
        };

        class C
        {
        public:
            C();
            ~C();
        };
    }
}

file B.h:

  #include "A.h"

    namespace Main {
        namespace ModuleB {

            class B
            {
                using A = Main::ModuleA::A;
                using A = ModuleA::C;

            public:
                B();
                ~B();
                void foo(A a);
                void bar(C c);
            };
        }
    }

Upvotes: 1

Views: 2354

Answers (2)

ObliteratedJillo
ObliteratedJillo

Reputation: 5156

1) Avoid declaring 'using namespace' to avoid conflict ( eg init() here will conflict ). Although class member functions will not suffer from name conflict, however public helper functions could. Similar named helper functions eases in loading inherited objects together, such as in factory design pattern, and namespace will become mandatory in such case.

2) Avoid using suffix namespace as it is implied.

3) Use namespace for clarity and to avoid conflict ( eg core::module::create_factory() makes it lucid)

namespace core{
namespace module1 {

       class MyClass1 {
           void method(Foo)
              {
                 module2::init();
              }
       };
   //HelperFunction
     void init();

}
}


namespace core{
namespace module2{

   class MyClass2 {
          void method(Foo)
              {
                 module1::init();
              }
   };
     //HelperFunction
     void init();
}
}

Upvotes: 0

user7860670
user7860670

Reputation: 37468

The better approach would be to declare class-level type alias for Foo and to remove using declaration from namespace scope. This will help to prevent name collisions when other classes from ModuleNamespace decide to use Foo from somewhere else.

class MyClass {
   using Foo = DifferentModuleNamespace::Foo;
   void method(Foo);
};

Alternatively, if Foo is supposed to be used in various other places of inside of ModuleNamespace then it would be worth to make a namespace-scope type alias (potentially residing in a separate header file):

// MainNamespace/ModuleNamespace/Foo.hpp
namespace MainNamespace {
namespace ModuleNamespace {

using Foo = DifferentModuleNamespace::Foo;

}
}

#include <MainNamespace/ModuleNamespace/Foo.hpp>

namespace MainNamespace {
namespace ModuleNamespace {

class MyClass {
     void method(Foo);
};

}
}

Upvotes: 2

Related Questions