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

问题描述

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

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

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

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

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]

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

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

由声明声明的名称被引入声明发生的范围,除了 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:

使用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.

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

05-27 18:24
查看更多