### 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;
    }

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.

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