在头文件和源代码 (cpp) 中创建 C++ 命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8210935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a C++ namespace in header and source (cpp)
提问by links77
Is there any difference between wrapping both header and cpp file contents in a namespace or wrapping just the header contents and then doing using namespacein the cpp file?
将头文件和 cpp 文件内容都包装在命名空间中或仅包装头内容然后在 cpp 文件中使用命名空间之间有什么区别吗?
By difference I mean any sort performance penalty or slightly different semantics that can cause problems or anything I need to be aware of.
我所说的差异是指任何可能导致问题或我需要注意的任何类型的性能损失或略有不同的语义。
Example:
例子:
// header
namespace X
{
class Foo
{
public:
void TheFunc();
};
}
// cpp
namespace X
{
void Foo::TheFunc()
{
return;
}
}
VS
VS
// header
namespace X
{
class Foo
{
public:
void TheFunc();
};
}
// cpp
using namespace X;
{
void Foo::TheFunc()
{
return;
}
}
If there is no difference what is the preferred form and why?
如果没有区别,首选形式是什么,为什么?
采纳答案by vprajan
Namespace is just a way to mangle function signature so that they will not conflict. Some prefer the first way and other prefer the second version. Both versions do not have any effect on compile time performance. Note that namespaces are just a compile time entity.
命名空间只是一种修改函数签名的方法,这样它们就不会发生冲突。有些人更喜欢第一种方式,而另一些人则更喜欢第二种方式。这两个版本对编译时性能没有任何影响。请注意,命名空间只是一个编译时实体。
The only problem that arises with using namespace is when we have same nested namespace names (i.e) X::X::Foo
. Doing that creates more confusion with or without using keyword.
使用命名空间出现的唯一问题是我们有相同的嵌套命名空间名称(即)X::X::Foo
。这样做会在使用或不使用关键字时造成更多混淆。
回答by Roee Gavirel
The difference in "namespace X" to "using namespace X" is in the first one any new declarations will be under the name space while in the second one it won't.
“命名空间 X”与“使用命名空间 X”的区别在于,在第一个中,任何新声明都将在命名空间下,而在第二个中则不会。
In your example there are no new declaration - so no difference hence no preferred way.
在你的例子中没有新的声明 - 所以没有区别,因此没有首选的方式。
回答by Michael Krelin - hacker
There's no performance penalties, since the resulting could would be the same, but putting your Foo
into namespace implicitly introduces ambiguity in case you have Foo
s in different namespaces. You can get your code fubar, indeed. I'd recommend avoiding using using
for this purpose.
没有性能损失,因为结果可能是相同的,但是Foo
如果您Foo
在不同的名称空间中有s,则将您的into 名称空间隐式地引入歧义。你确实可以得到你的代码 fubar。我建议避免using
为此目的使用。
And you have a stray {
after using namespace
;-)
你有一个流浪{
之后using namespace
;-)
回答by AlexTheo
In case if you do wrap only the .h content you have to write using namespace ... in cpp file otherwise you every time working on the valid namespace. Normally you wrap both .cpp and .h files otherwise you are in risk to use objects from another namespace which may generate a lot of problems.
如果您只包装 .h 内容,您必须使用 namespace ... 在 cpp 文件中编写,否则您每次都在有效的命名空间上工作。通常你同时包装 .cpp 和 .h 文件,否则你有使用来自另一个命名空间的对象的风险,这可能会产生很多问题。
回答by holgac
If the second one compiles as well, there should be no differences. Namespaces are processed in compile-time and should not affect the runtime actions.
如果第二个也编译,应该没有区别。命名空间在编译时处理,不应影响运行时操作。
But for design issues, second is horrible. Even if it compiles (not sure), it makes no sense at all.
但是对于设计问题,第二个是可怕的。即使它编译(不确定),它也没有任何意义。
回答by bert-jan
The Foo::TheFunc() is not in the correct namespacein the VS-case. Use 'void X::Foo::TheFunc() {}' to implement the function in the correct namespace (X).
Foo::TheFunc() 不在 VS 案例中的正确命名空间中。使用 'void X::Foo::TheFunc() {}' 在正确的命名空间 (X) 中实现该函数。
回答by Kjetil Hvalstrand
I think right thing to do here is to use namespace for scoping.
我认为这里正确的做法是使用命名空间进行范围界定。
namespace catagory
{
enum status
{
none,
active,
paused
}
};
void func()
{
catagory::status status;
status = category::active;
}