编程语言

img lynnboy

3.3 - [basic.scope] - 【基本.作用域】

发表于2004/11/2 15:55:00  2275人阅读

请不要转载本文;请不要以任何形式重新出版,发布本文;请在下载本文 24 小时内将其删除;禁止将本文用于商业目的。

3 Basic concepts [basic]

3.3 Declarative regions and scopes [basic.scope]

 

3 基本概念 【基本】

3.3 声明区域和作用域 【基本.作用域】

 

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. In general, each particular name is valid only within some possibly discontiguous portion of program text called its scope. To determine the scope of a declaration, it is sometimes convenient to refer to the potential scope of a declaration. The scope of a declaration is the same as its potential scope unless the potential scope contains another declaration of the same name. In that case, the potential scope of the declaration in the inner (contained) declarative region is excluded from the scope of the declaration in the outer (containing) declarative region.

 

每个名字都被引入到程序文本中某些被称为声明区域的部分中,声明区域是程序中该名字有效的最广泛区域,也就是说,这个名字在其中可以作为非限定名应用并指代同一个实体。通常情况下,每个特定的名字仅在程序中某些被称为该名字的作用域的不连续区域中有效。为确定一个声明的作用域,通常为方便而提到声明的潜在作用域。声明的作用域与其潜在作用域相同,除非潜在作用域包含相同名字的另一个声明。此时,内部(被包含)的声明区域中声明的潜在作用域被外部(包含的)声明区域中声明的作用域排除。

 

[Example: in

    int j = 24;
    int main()
    {
        int i = j, j;
        j = 42;
    }

the identifier j is declared twice as a name (and used twice). The declarative region of the first j includes the entire example. The potential scope of the first j begins immediately after that j and extends to the end of the program, but its (actual) scope excludes the text between the , and the }. The declarative region of the second declaration of j (the j immediately before the semicolon) includes all the text between { and }, but its potential scope excludes the declaration of i. The scope of the second declaration of j is the same as its potential scope. ]

 

例:如下

    int j = 24;
    int main()
    {
        int i = j, j;
        j = 42;
    }

标识符 j 作为名字被声明两次(并被应用两次)。第一个 j 的声明区域包括整个程序。第一个 j 的潜在作用域从 j 的声明立刻开始,并展开到程序结尾,但其(实际的)作用域排除了从 ,} 之间的文本。第二个 j 声明(紧挨着分号的 j)的声明区域包括从 {} 之间的所有文本,但其潜在作用域不包括 i 的声明。第二个 j 的作用域与其潜在作用域相同。】

 

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.4), certain uses of the elaborated-type-specifier (3.3.1), and using-directives (7.3.4) alter this general behavior.

 

声明将名字引入该声明发生的作用域中,除非声明具有 friend 限定词(11.4),包含详细类型限定(3.3.1)的特定用法,或使用指令(7.3.4)来改变其一般行为。

 

Given a set of declarations in a single declarative region, each of which specifies the same unqualified name,
  • they shall all refer to the same entity, or all refer to functions and function templates; or
  • exactly one declaration shall declare a class name or enumeration name that is not a typedef name and the other declarations shall all refer to the same object or enumerator, or all refer to functions and function templates; in this case the class name or enumeration name is hidden (3.3.7). [Note: a namespace name or a class template name must be unique in its declarative region (7.3.2, clause 14). ]

[Note: these restrictions apply to the declarative region into which a name is introduced, which is not necessarily the same as the region in which the declaration occurs. In particular, elaborated-type-specifiers (3.3.1) and friend declarations (11.4) may introduce a (possibly not visible) name into an enclosing namespace; these restrictions apply to that region. Local extern declarations (3.5) may introduce a name into the declarative region where the declaration appears and also introduce a (possibly not visible) name into an enclosing namespace; these restrictions apply to both regions. ]

 

若给定某单个声明区域中指定相同非限定名的一组声明,
  • 它们应该全部指代相同实体,或全部指代函数或函数模板;或者
  • 仅能有一个不是 typedef 名字的类名称或枚举名称的声明,而其他声明应该全部指代相同对象或枚举符,或全部指代函数和函数模板;此时类名或枚举名被隐藏(3.3.7)。【注:名字空间名或类模板名在其声明区域中必须唯一(7.3.2,章节 14)。】

【注:这些限制应用于名字被引入其中的声明区域,该区域不必和声明发生的声明区域相同。特别地,详细类型限定词(3.3.1)和友元声明(11.4)能够向包含其的名字空间中引入一个(可能不可见)的名字;这些限制就应用到这个区域。局部外部声明(3.5)能够向其所在的声明区域引入名字,也能向包含它的名字空间中引入(可能不可见的)名字;这些限制对这两个区域都有效。】

 

[Note: the name lookup rules are summarized in 3.4. ]

 

注:3.4 中概述了名字查找规则。】

 

3.3.1 Point of declaration [basic.scope.pdecl]

 

3.3.1 声明点 【基本.作用域.声明点】

 

3.3.2 Local scope [basic.scope.local]

 

3.3.2 局部作用域 【基本.作用域.局部】

 

3.3.3 Function prototype scope [basic.scope.proto]

 

3.3.3 函数原型作用域 【基本.作用域.原型】

 

3.3.4 Function scope [basic.funscope]

 

3.3.4 函数作用域 【基本.函数作用域】

 

3.3.5 Namespace scope [basic.scope.namespace]

 

3.3.5 名字空间作用域 【基本.作用域.名字空间】

 

3.3.6 Class scope [basic.scope.class]

 

3.3.6 类作用域 【基本.作用域.类】

 

3.3.7 Name hiding [basic.scope.hiding]

 

3.3.7 名字隐藏 【基本.作用域.隐藏】

 

PREV [basic.def.odr] | NEXT [basic.scope.pdecl] 上一页 【基本.定义.ODR】 | 下一页 【基本.作用域.声明点】
阅读全文
0 0

相关文章推荐

img
取 消
img