声明名称,引入名称和声明实体之间的区别

Sup*_*mum 7 c++ entity declaration using-declaration language-lawyer

从C++ 11标准,§7.3.3[namespace.udecl]/1:

using声明在声明区域中引入了一个名称,其中出现using声明.

使用声明:

using typenameopt nested-name-specifier unqualified-id ;
using :: unqualified-id ;

using声明中指定的成员名称在using声明出现的声明区域中声明.

在使用声明发生的声明性区域中声明的名称是什么意思?

这是否意味着将该名称引入发生using声明的声明性区域?

声明名称和声明名称所代表的实体之间是否有区别?

例:

namespace N { static int i = 1; } /* Declares an entity denoted by 
    the name i in the declarative region of the namespace N. 
    Introduces the name into the declarative region of the namespace N.
    Declares the name i in the declarative region of the namespace N? */
using N::i; /* Declares the name i in the declarative region of the
    global namespace. Also introduces that name into the declarative
    region of the global namespace? Also declares the entity that the
    name i denotes? */ 
Run Code Online (Sandbox Code Playgroud)

Bar*_*rry 6

从第一原则来看,一个实体来自[基本]

值,对象,引用,函数,枚举器,类型,类成员,位字段,模板,模板特化,命名空间,参数包或this.[...]表示实体的每个名称都由声明引入.

声明声明事物.要声明它是由声明引入的,来自[basic.scope.declarative]

每个名称都在程序文本的某些部分中引入,称为声明性区域,该声明区域是该名称有效的程序的最大部分,也就是说,该名称可以用作非限定名称以引用同一实体.

声明声明的名称被引入声明发生的范围,除了说明friend符(11.3)的存在,精心设计类型说明符(7.1.6.3)的某些用法和 使用指令(7.3. 4)改变这种一般行为.

这些例外都不相关,因为我们讨论的是使用声明而不是使用指令.让我稍微改变你的例子,以避免全局命名空间:

namespace N {        //  + declarative region #1
                     //  |
    static int i;    //  | introduces a name into this region
                     //  | this declaration introduces an entity
}                    //  +
Run Code Online (Sandbox Code Playgroud)

首先,N::i是一个在命名空间中声明N并引入范围的实体N.现在,让我们添加一个using声明:

namespace B {        //  + declarative region #2
                     //  |
    using N::i;      //  | declaration introduces a name i
                     //  | but this is not an entity
}                    //  +
Run Code Online (Sandbox Code Playgroud)

从[namespace.udecl],我们有:

如果using声明命名一个构造函数(3.4.3.1),它会隐式声明出现using声明的类中的一组构造函数(12.9); 否则,using-declaration中指定的名称是另一个名称空间或类中的一组声明的 同义词.

使用声明 using N::i没有指定构造函数,因此而不是名称i是一个新的实体,它是不是一个代名词N::i.

所以基本上,两个is都是在各自的命名空间中引入和声明的名称.在N,i声明具有静态链接的实体,但在B,i声明该实体的同义词 - 而不是新实体.