声明名称,引入名称和声明实体之间的区别 [英] The difference between declaring a name, introducing a name, and declaring an entity

查看:59
本文介绍了声明名称,引入名称和声明实体之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据C ++ 11标准,第7.3.3节[namespace.udecl]/1:

From the C++11 standard, §7.3.3[namespace.udecl]/1:

使用声明将名称引入显示使用声明的声明区域.

A using-declaration introduces a name into the declarative region in which the using-declaration appears.

使用声明:

使用类型名 opt 嵌套名称说明符unqualified-id ;
using :: unqualified-id ;

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

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

在使用声明出现的声明区域中声明名称是什么意思?

What do they mean by the name being declared in the declarative region where the using-declaration occurs?

这是否等同于将该名称引入使用声明发生的声明区域中?

Does this mean the same as introducing that name into the declarative region where the using-declaration occurs?

声明名称和声明名称所表示的实体之间也有区别吗?

Also is there a difference between declaring a name and declaring the entity that the name denotes?

示例:

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? */ 

推荐答案

从最初的原理出发,实体来自[基本]

From first principles, an entity is, from [basic]

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

a value, object, reference, function, enumerator, type, class member, bit-field, template, template specialization, namespace, parameter pack, or this. [...] Every name that denotes an entity is introduced by a declaration.

声明声明事物.要声明意味着它是由[basic.scope.declarative]

Declarations declare things. To be declared means that it was introduced by a declaration, from [basic.scope.declarative]

每个名称都会在程序文本的某些部分引入,称为声明性区域,这是最大的部分该名称有效的程序的名称,即该名称可以用作不合格名称的程序指代同一实体.

Every name is introduced in some portion of program text called a declarative region, which is the largest part of the program in which that name is valid, that is, in which that name may be used as an unqualified name to refer to the same entity.

由声明声明的名称被引入声明发生的范围,除了 friend 说明符(11.3)的存在,精化类型说明符(7.1.6.3)的某些用法,以及 using-directives (7.3.4)会改变这种一般行为.

The names declared by a declaration are introduced into the scope in which the declaration occurs, except that the presence of a friend specifier (11.3), certain uses of the elaborated-type-specifier (7.1.6.3), and using-directives (7.3.4) alter this general behavior.

所有这些异常都与此处无关,因为我们在谈论的是 using-declarations ,而不是 using-directives .让我对您的示例进行一些更改,以避免使用全局名称空间:

None of those exceptions are relevant here, since we're talking about using-declarations and not using-directives. Let me alter your example somewhat so as to avoid the global namespace:

namespace N {        //  + declarative region #1
                     //  |
    static int i;    //  | introduces a name into this region
                     //  | this declaration introduces an entity
}                    //  +

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

So to start with, N::i is an entity that is declared in namespace N and introduced into the scope of N. Now, let's add a using-declaration:

namespace B {        //  + declarative region #2
                     //  |
    using N::i;      //  | declaration introduces a name i
                     //  | but this is not an entity
}                    //  +

在[namespace.udecl]中,我们有:

From [namespace.udecl], we have:

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

If a using-declaration names a constructor (3.4.3.1), it implicitly declares a set of constructors in the class in which the using-declaration appears (12.9); otherwise the name specified in a using-declaration is a synonym for a set of declarations in another namespace or class.

使用N :: i的使用声明 不命名构造函数,因此与其将名称 i 用作新实体,而是 N :: i 同义词.

The using-declaration using N::i does not name a constructor, so rather than having the name i be a new entity, it is instead a synonym for N::i.

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

So basically, both is are names introduced in and declared in their respective namespaces. In N, i declares an entity with static linkage, but in B, i declares a synonym to that entity - not a new entity.

这篇关于声明名称,引入名称和声明实体之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆