来自ISO C+的一个点+;草稿(n3290):3.4.3.2/1命名空间成员 一个点,从ISOC++草案(N32 90):3.4.3.2/1命名空间成员

来自ISO C+的一个点+;草稿(n3290):3.4.3.2/1命名空间成员 一个点,从ISOC++草案(N32 90):3.4.3.2/1命名空间成员,c++,namespaces,c++11,C++,Namespaces,C++11,如果限定id的嵌套名称说明符指定命名空间, 在中查找嵌套名称说明符后指定的名称 命名空间的作用域如果限定id以::开头,则 在全局命名空间中查找::后的名称。在任何一种情况下 在这种情况下,将查找模板id的模板参数中的名称 在整个后缀表达式出现的上下文中。 这里有谁能解释一下粗体部分。。。。从早期的c++03草稿到c++0x草稿,他添加了 如果限定id以::开头,则查找::后面的名称 在全局命名空间中。 任何人都可以用一个示例程序解释一下吗?::S是一个合格的id 在限定id::S::f中,S:

如果限定id的嵌套名称说明符指定命名空间, 在中查找嵌套名称说明符后指定的名称 命名空间的作用域如果限定id以::开头,则 在全局命名空间中查找::后的名称。在任何一种情况下 在这种情况下,将查找模板id的模板参数中的名称 在整个后缀表达式出现的上下文中。

这里有谁能解释一下粗体部分。。。。从早期的c++03草稿到c++0x草稿,他添加了

如果限定id以::开头,则查找::后面的名称 在全局命名空间中。


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

::S
是一个合格的id

在限定id
::S::f
中,
S::
是嵌套的名称说明符

在非正式术语中,嵌套名称说明符是

  • 在限定id的最开始处开始,或者在初始范围解析运算符(::)之后开始(如果在id的最开始处出现),并且
  • 以限定id中的最后一个作用域解析运算符结束
非常非正式地说,id是限定id或非限定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
    是一个限定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;
}
#包括
使用名称空间std;
整数计数(0);//用于迭代
类外部{
公众:
static int count;//统计外部类的数量
班级内部{
公众:
static int count;//计算内部类的数量
};
};
int外部::计数(42);//假设有42个外部类
int外部::内部::计数(32768);//假设有2^15个内部类
//掌握窍门了吗?
int main(){
//我们如何获取这些号码?
//
//使用“count=?”是非常不明确的,因为我们不清楚是哪个
//我们指的是伯爵。
//
//嵌套名称说明符在这里帮助我们

cout它被称为限定名称查找
前导的
::
引用全局命名空间。任何以
::
开头的限定标识符将始终引用全局命名空间中的某个标识符,而不是本地命名空间中的同一命名标识符

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();
   }
}

链接文本指的是两种不同的情况。第一部分是使用或不使用<代码>::作为前缀的区分。当限定名从“代码>::/COD>开始时,从空的命名空间开始检查确切的命名空间,而如果不存在,则搜索将考虑嵌套的名称空间:

namespace A {
    void f() { std::cout << "::A::f" << std::endl; }
}
namespace B {
    namespace A {
        void f() { std::cout << "::B::A::f" << std::endl; }
    }
    void g() {
        A::f();     // ::B::A::f
        ::A::f();   // ::A::f
    }
}
名称空间A{

void f(){std::cout这部分怎么办“无论哪种情况,模板id的模板参数中的名称都是在整个后缀表达式出现的上下文中查找的。”…请解释一下,我恐怕这不是一个答案->“任何人都可以用示例程序解释吗”@phresnel:别害怕,现在它是一个答案:)
struct A {
    static void f() { std::cout << "::A::f()" << std::endl; }
};
template <typename T>
void f() {
    T::f();
}
namespace N {
    struct A {
        static void f() { std::cout << "::N::A::f()" << std::endl; }
    };
    void g() {
        f<A>();
    }
}
int a(1);

class someCls {
  private:
    int a;
  public:
    // Assigns this->a with the value of the global variable ::a.
    void assignFromGlobal() {
      a = ::a;
    }
};