C ++未命名(匿名)名称空间定义 [英] C++ unnamed(anonymous) namespace definition
问题描述
C ++ 03 Standard 7.3.1.1 [namespace.unnamed]第1段:(和C ++ 11 Standard也使用类似的定义)
C++03 Standard 7.3.1.1 [namespace.unnamed] paragraph 1: (and C++11 Standard also use similar definition)
未命名空间定义的行为就好像被替换为
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;
侧面问题: MSDN 由后一种形式定义.它在技术上是否违反标准?
Side question: MSDN defines by latter form. Does it violate Standard technically?
推荐答案
您不能再这样做
namespace { typedef int a; ::a x; }
请注意,在随后的namespace { ... }
中,您突然可以.这将是非常不一致的.
Note that in a subsequent namespace { ... }
, suddenly you could. 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);
}
}
对于ISO C ++,这将调用f
的int
版本.使用您的替代定义,它称为long
版本.
With ISO C++, this calls the int
version of f
. With your alternative definition, it calls the long
version.
这篇关于C ++未命名(匿名)名称空间定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!