C++ 为什么未命名的命名空间是静态的“高级”替代方案?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4977252/
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
Why an unnamed namespace is a "superior" alternative to static?
提问by Nawaz
The section $7.3.1.1/2 from the C++ Standard reads:
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 关键字;未命名命名空间提供了一个更好的选择。
I don't understand why an unnamed namespace is considered a superior alternative? What is the rationale? I've known for a long time as to what the standard says, but I've never seriously thought about it, even when I was replying to this question: Superiority of unnamed namespace over static?
我不明白为什么未命名的命名空间被认为是更好的选择?理由是什么?我早就知道标准所说的内容,但我从来没有认真考虑过,即使我在回答这个问题时:未命名命名空间优于静态?
Is it considered superior because it can be applied to user-defined types as well, as I described in my answer? Or is there some other reason as well, that I'm unaware of? I'm asking this, particularly because that is my reasoning in my answer, while the standard might have something else in mind.
正如我在回答中所描述的那样,它是否被认为是优越的,因为它也可以应用于用户定义的类型?还是还有其他一些我不知道的原因?我问这个,特别是因为这是我在回答中的推理,而标准可能有其他想法。
回答by Sergei Tachenov
- As you've mentioned, namespace works for anything, not just for functions and objects.
- As Greg has pointed out,
static
means too many things already. - Namespaces provide a uniform and consistent way of controlling visibility at the global scope. You don't have to use different tools for the same thing.
- When using an anonymous namespace, the function/object name will get mangled properly, which allows you to see something like "(anonymous namespace)::xyz" in the symbol table after de-mangling, and not just "xyz" with static linkage.
- As pointed out in the comments below, it isn't allowed to use static things as template arguments, while with anonymous namespaces it's fine.
- More? Probably, but I can't think of anything else right now.
- 正如您所提到的,命名空间适用于任何事物,而不仅仅是适用于函数和对象。
- 正如格雷格所指出的,这
static
意味着太多的东西。 - 命名空间提供了一种在全局范围内控制可见性的统一和一致的方式。您不必为同一件事使用不同的工具。
- 使用匿名命名空间时,函数/对象名称将被正确修改,这允许您在去修改后在符号表中看到类似“(匿名命名空间):: xyz”的内容,而不仅仅是带有静态链接的“xyz” .
- 正如下面的评论中所指出的,不允许使用静态事物作为模板参数,而使用匿名命名空间就可以了。
- 更多的?可能吧,但我现在想不出别的了。
回答by Greg Hewgill
One reason may be that static
already has too many meanings (I can count at least three). Since an anonymous namespace can encapsulate anything including types, it seems superior to the static
solution.
一个原因可能是static
已经有太多的含义(我至少可以数出三个)。由于匿名命名空间可以封装包括类型在内的任何内容,因此它似乎优于static
解决方案。
回答by Matthieu M.
There are two reasons I think:
我认为有两个原因:
static
has two different meanings: at class scope, it means shared by the whole class while at file/function scope it affects the visibility/storage...- unnamed namespaces allow to declare new
struct
,class
andtypedef
static
有两种不同的含义:在类范围内,它意味着由整个类共享,而在文件/函数范围内,它会影响可见性/存储...- 未命名的命名空间允许声明 new
struct
,class
以及typedef
One note though, the commitee backpedaled on this: static
is no longer marked as deprecated in n3225
.
但是,有一个注意事项,委员会对此进行了退让:static
在n3225
.