简体   繁体   English

C ++未命名(匿名)命名空间定义

[英]C++ unnamed(anonymous) namespace definition

C++03 Standard 7.3.1.1 [namespace.unnamed] paragraph 1: (and C++11 Standard also use similar definition) C ++ 03标准7.3.1.1 [namespace.unnamed]第1段:(和C ++ 11标准也使用相似的定义)

An unnamed-namespace-definition behaves as if it were replaced by 未命名的命名空间定义的行为就像被替换为

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

Why not is it simply following definition? 为什么不简单地遵循定义?

namespace unique { namespace-body }
using namespace unique;

Side question: MSDN defines by latter form. 附带问题: MSDN按后一种形式定义。 Does it violate Standard technically? 它在技术上是否违反了标准?

You could not do this anymore 你不能再这样做了

namespace { typedef int a; ::a x; }

Note that in a subsequent namespace { ... } , suddenly you could. 请注意,在后续的namespace { ... } ,突然间你可以。 This would be horribly inconsistent. 这将是非常不一致的。

Also notice this case, with two different valid outcomes 还要注意这种情况,有两种不同的有效结果

namespace A { void f(long); }
using namespace A;

namespace { 
  void f(int);
  void g() {
    ::f(0);
  }
}

With ISO C++, this calls the int version of f . 使用ISO C ++,这将调用fint版本。 With your alternative definition, it calls the long version. 使用您的替代定义,它会调用long版本。

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

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