C++ 未命名命名空间优于静态命名空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4422507/
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
Superiority of unnamed namespace over static?
提问by Nawaz
How are unnamed namespaces superior to the static
keyword?
未命名的命名空间如何优于static
关键字?
回答by Nawaz
You're basically referring to the section $7.3.1.1/2 from the C++ Standard,
您基本上是指 C++ 标准中的 $7.3.1.1/2 部分,
The use of the static keyword is deprecated when declaring objects in a namespace scope; the unnamed-namespace provides a superior alternative.
在命名空间范围内声明对象时,不推荐使用 static 关键字;未命名命名空间提供了一个更好的选择。
Unnamed namespace is superior to static keyword, primarily because the keyword static
applies only to the variablesdeclarations and functions, not to the user-defined types.
未命名命名空间优于 static 关键字,主要是因为该关键字static
仅适用于变量声明和函数,而不适用于用户定义的类型。
The following code is valid in C++
以下代码在 C++ 中有效
//legal code
static int sample_function() { /* function body */ }
static int sample_variable;
But this code is NOT valid:
但此代码无效:
//illegal code
static class sample_class { /* class body */ };
static struct sample_struct { /* struct body */ };
So the solution is, unnamed-namespace, which is this,
所以解决方案是,未命名的命名空间,就是这样,
//legal code
namespace
{
class sample_class { /* class body */ };
struct sample_struct { /* struct body */ };
}
Hope it explains that why unnamed-namespace
is superior to static
.
希望它能解释为什么unnamed-namespace
优于static
.
Also, note that use of static keyword is deprecated when declaring objects in a namespace scope (as per the Standard).
另请注意,在命名空间范围内声明对象时,不推荐使用 static 关键字(根据标准)。
回答by SasQ
There's an interesting problem related to this:
有一个与此相关的有趣问题:
Suppose you use static
keyword or unnamed namespace
to make some function internal to the module (translation unit), since this function is meant to be used internally by the module and not accessible outside of it. (Unnamed namespace
s have the advantage of making data and type definitions internal, too, besides functions).
假设您使用static
关键字或未命名namespace
来创建模块内部的某些函数(翻译单元),因为该函数旨在由模块内部使用,并且无法在模块外部访问。(namespace
除了函数之外,未命名的s 还具有使数据和类型定义成为内部的优点)。
With time the source file of the implementation of your module grows large, and you would like to split it into several separate source files, which would allow for better organizing the code, finding the definitions quicker, and to be compiled independently.
随着时间的推移,模块实现的源文件会变得越来越大,您希望将其拆分为几个单独的源文件,这样可以更好地组织代码、更快地找到定义并独立编译。
But now you face a problem: Those functions can no longer be static
to the module, because static
doesn't actually refer to the module, but to the source file(translation unit). You are forced to make them non-static
to allow them to be accessed from other parts (object files) of that module. But this also means that they are no longer hidden/private to the module: having external linkage, they can be accessed from other modules, which was notyour original intention.
但是现在你面临一个问题:那些函数不能再static
指向模块了,因为static
实际上并不是指向模块,而是指向源文件(翻译单元)。您被迫将它们static
设为非,以允许从该模块的其他部分(目标文件)访问它们。但这也意味着它们不再对模块隐藏/私有:具有外部链接,可以从其他模块访问它们,这不是您的初衷。
Unnamed namespace
wouldn't solve this problem either, because it is also defined for a particular source file (translation unit) and cannot be accessed from outside.
Unnamednamespace
也不能解决这个问题,因为它也是为特定的源文件(翻译单元)定义的,不能从外部访问。
It would be great if one could specify that some namespace
is private
, that is, whatever is defined in it, is meant to be used internally by the module it belongs to. But of course C++ doesn't have such concept as "modules", only "translation units", which are tightly bound to the source files.
如果可以指定 some namespace
is private
,即其中定义的任何内容,都应该由它所属的模块在内部使用,那就太好了。但当然 C++ 没有“模块”这样的概念,只有“翻译单元”,它们与源文件紧密绑定。
回答by Salgar
The C++ Standard reads in section 7.3.1.1 Unnamed namespaces, paragraph 2:
C++ 标准在第 7.3.1.1 节未命名命名空间,第 2 段中读到:
The use of the static keyword is deprecated when declaring objects in a namespace scope, the unnamed-namespace provides a superior alternative.
在命名空间范围内声明对象时不推荐使用 static 关键字,未命名命名空间提供了一个更好的选择。
Static only applies to names of objects, functions, and anonymous unions, not to type declarations.
静态仅适用于对象、函数和匿名联合的名称,不适用于类型声明。