[英]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. 我们在代码中有一个常见的命名空间
MainNamespace
,每个模块都有很多命名空间,例如。 ModuleNamespace
, DifferentModuleNamespace
. 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: 或从路径中删除
MainNamespace
命名空间:
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;
). 使用绝对路径到名称空间时是否有任何情况(
using A = Main::ModuleA::A;
)会比使用短路径( using A = ModuleA::C;
)更安全。 When we do it in the same main namespace. 当我们在相同的主命名空间中执行此操作时。
file Ah: 文件啊:
namespace Main
{
namespace ModuleA {
class A
{
public:
A();
~A();
};
class C
{
public:
C();
~C();
};
}
}
file Bh: 文件Bh:
#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);
};
}
}
The better approach would be to declare class-level type alias for Foo
and to remove using
declaration from namespace scope. 更好的方法是为
Foo
声明类级别的类型别名,并从命名空间范围中删除using
声明。 This will help to prevent name collisions when other classes from ModuleNamespace
decide to use Foo
from somewhere else. 当
ModuleNamespace
其他类决定从其他地方使用Foo
时,这将有助于防止名称冲突。
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): 或者,如果应该在
ModuleNamespace
内部的其他各个地方使用Foo
,则值得创建一个名称空间范围类型的别名(可能位于单独的头文件中):
// 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);
};
}
}
1) Avoid declaring 'using namespace' to avoid conflict ( eg init() here will conflict ). 1)避免声明“使用名称空间”以避免冲突(例如init()会发生冲突)。 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. 2)避免使用隐含的后缀名称空间。
3) Use namespace for clarity and to avoid conflict ( eg core::module::create_factory() makes it lucid) 3)使用名称空间来保持清晰并避免冲突(例如core :: module :: create_factory()使它变得清晰)
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.