简体   繁体   English

匿名命名空间

[英]Anonymous Namespace

A recent thread on SO triggerred this. SO上最近的一个帖子触发了这一点。

An anonymous namespace is considered to be equivalent to 匿名命名空间被认为是等效的

  namespace unique { /* empty body */ } 
  using namespace unique; 
  namespace unique { namespace-body }

I fail to recollect the exact reason as to why it is not equivalent to 我没有回想起为什么它不等同于它的确切原因

  namespace unique { namespace-body } 
  using namespace unique;

Also tried searching (including google) but in vain. 也尝试搜索(包括谷歌),但徒劳无功。 Please share any information you have in this regards. 请分享您在此方面的任何信息。

The specification that exists now was introduced in 1995 in N0783 to correct for a corner case. 现在存在的规范是在1995年在N0783中引入的,用于纠正极端情况。 To quote that paper (page 9): 引用该论文(第9页):

The WP defines the semantics of an unnamed namespace as being equivalent to: WP将未命名的命名空间的语义定义为等效于:

 namespace UNIQUE { // namespace body } using namespace UNIQUE; 

This is incorrect because it makes the code in an unnamed namespace dependent on whether the code is in an original namespace or a namespace extension. 这是不正确的,因为它使未命名的命名空间中的代码取决于代码是在原始命名空间还是命名空间扩展中。

 namespace {} // If you remove this line, the // use of ::f below is invalid namespace { void f() { using ::f; } } 

The WP should be changed to define an unnamed namespace as being equivalent to: 应更改WP以将未命名的命名空间定义为等效于:

 namespace UNIQUE {} using namespace UNIQUE; namespace UNIQUE { // namespace body } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM