来自 ISO C++ 草案 (n3290) 的一点:3.4.3.2/1 命名空间成员
If the nested-name-specifier of a qualified-id nominates a namespace, the name specified after the nested-name-specifier is looked up in the scope of the namespace. If a qualified-id starts with ::, the name after the :: is looked up in the global namespace. In either case, the names in a template-argument of a template-id are looked up in the context in which the entire postfix-expression occurs.
这里任何人都可以解释一下粗体部分....以及他添加的从早期的 c++03 草案到 c++0x 草案
If a qualified-id starts with ::, the name after the :: is looked up in the global namespace.
谁能用示例程序解释一下
最佳答案
::S
是一个合格的id。
在限定 ID ::S::f
, S::
是嵌套名称说明符。
在非正式术语中,嵌套名称说明符是 id 的一部分
- 开始于合格 id 的最开始或在初始范围解析运算符 (::) 之后,如果它出现在 id 的最开始,并且
- 以 qualified-id 中的最后一个范围解析运算符结束。
非常通俗地说,一个 id 要么是一个 qualified-id 要么是一个 unqualified-id。如果 id 是一个合格的 id,它实际上由两部分组成:一个嵌套名称说明符,后跟一个非限定的 id。
给定:
struct A {
struct B {
void F();
};
};
-
A
是一个不合格的 id。 -
::A
是限定 ID 但没有嵌套名称说明符。 -
A::B
是一个合格的 ID 和A::
是嵌套名称说明符。 -
::A::B
是一个合格的 ID 和A::
是嵌套名称说明符。 -
A::B::F
是一个 qualified-id 并且都是B::
和A::B::
是嵌套名称说明符。 -
::A::B::F
是一个 qualified-id 并且都是B::
和A::B::
是嵌套名称说明符。
另一个例子:
#include <iostream>
using namespace std;
int count(0); // Used for iteration
class outer {
public:
static int count; // counts the number of outer classes
class inner {
public:
static int count; // counts the number of inner classes
};
};
int outer::count(42); // assume there are 42 outer classes
int outer::inner::count(32768); // assume there are 2^15 inner classes
// getting the hang of it?
int main() {
// how do we access these numbers?
//
// using "count = ?" is quite ambiguous since we don't explicitly know which
// count we are referring to.
//
// Nested name specifiers help us out here
cout << ::count << endl; // The iterator value
cout << outer::count << endl; // the number of outer classes instantiated
cout << outer::inner::count << endl; // the number of inner classes instantiated
return 0;
}
编辑:
作为对您的评论的回应,我认为该声明只是意味着模板的参数是在声明它们的上下文和行中处理的。例如,
在f.~foo();
, foo 在 f.
内查找, 并在 foo<int>
的范围内, 用 foo
引用它是有效的.
关于c++ - 来自 ISO C++ draft (n3290) : 3. 4.3.2/1 命名空间成员的一点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7006938/