ISO C++草案(n3290)中的一点:3.4.3.2/1命名空间成员

use*_*747 4 c++ namespaces c++11

ISO C++草案(n3290)中的一点:3.4.3.2/1命名空间成员

如果qualified-id的nested-name-specifier指定了名称空间,则在名称空间的范围内查找在嵌套名称说明符之后指定的名称.如果qualified-id以::开头,则在全局命名空间中查找::之后的名称.在任何一种情况下,都会在发生整个后缀表达式的上下文中查找template-id的template-argument中的名称.

这里可以解释一下BOLD部分......以及从早期的c ++ 03草案到c ++ 0x草案他补充道

如果qualified-id以::开头,则在全局命名空间中查找::之后的名称.

任何人都可以用示例程序解释一下

小智 5

::S 是一个合格的身份证.

在qualified-id中::S::f,S::是一个嵌套名称说明符.

在非正式术语中,嵌套名称说明符是id的一部分

  • 从一个qualified-id的最开始或在初始范围解析运算符(::)之后开始,如果一个出现在id的最开头,
  • 以qualified-id中的最后一个范围解析运算符结束.

非正式地,id是qual-id或nonqualified-id.如果id是qualified-id,它实际上由两部分组成:嵌套名称说明符后跟unqualified-id.

鉴于:

struct  A {
    struct B {
        void F();
    };
};
Run Code Online (Sandbox Code Playgroud)
  • A 是一个不合格的身份证.
  • ::A 是一个限定ID,但没有嵌套名称说明符.
  • A::B是一个限定ID,A::是一个嵌套名称说明符.
  • ::A::B是一个限定ID,A::是一个嵌套名称说明符.
  • A::B::F是一个合格的-ID和两个B::A::B::嵌套的名称,说明符.
  • ::A::B::F是一个合格的-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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

在回应你的评论时,我认为该陈述只是意味着模板的参数是在声明它们的上下文和行的基础上处理的.例如,

in f.~foo();,foo在内部查找f.,并且在范围内foo<int>,只使用with引用它是有效的foo.


Alo*_*ave 5

它被称为合格名称查找.
前导::引用全局命名空间.以a开头的任何限定标识符::将始终通过本地命名空间中的相同命名标识符引用全局命名空间中的某个标识符.

namespace A
{ 
    namespace B
    {
        void doSomething();
    }
}

namespace Z 
{ 
    namespace A
    {  
        namespace B
        {
            void doSomething();
        } 
    }

    using namespace A::B // no leading :: refers to local namespace layer

    void doSomethingMore() 
    {
       doSomething(); // calls Z::A::B::doSomething();

    }
}

namespace Z
{
   namespace A
   { 
      namespace B
      {
          void doSomething();
      } 
   }

   using namespace ::A::B // leading :: refers to global namespace A
   void doSomethingMore() 
   {
        doSomething(); // calls ::A::B::doSomething();
   }
}
Run Code Online (Sandbox Code Playgroud)