c++ - 来自 ISO C++ draft (n3290) : 3. 4.3.2/1 命名空间成员的一点

标签 c++ namespaces c++11

来自 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/

相关文章:

c++ - 管理嵌套类

c++ - 如何制作一个 C++ 模板类,根据模板值的类更改其成员和访问器?

C++ - 在您自己的项目中使用您自己的命名空间

hadoop - HDFS fsck 命令输出

r - Ubuntu 上的 namespaceExports(ns,exports)错误,但不是 Windows

c++ - 如何编写具有高重载优先级的类标准函数

c++ - 如何改进 std::vector 参数传递( move 语义?)

c++ - 当用字符串文字实例化时,模板 T & 参数到底是什么?

c++ - 运算符<< 重载解析 (C++)

c++ - glColor3f 仅颜色为红色