Стандарт C++ 98, страница 10
Описание файла
PDF-файл из архива "Стандарт C++ 98", который расположен в категории "". Всё это находится в предмете "практикум (прикладное программное обеспечение и системы программирования)" из 4 семестр, которые можно найти в файловом архиве МГУ им. Ломоносова. Не смотря на прямую связь этого архива с МГУ им. Ломоносова, его также можно найти и в других разделах. .
Просмотр PDF-файла онлайн
Текст 10 страницы из PDF
The potential scope denoted by anoriginal-namespace-name is the concatenation of the declarative regions established by each of thenamespace-definitions in the same declarative region with that original-namespace-name. Entities declaredin a namespace-body are said to be members of the namespace, and names introduced by these declarationsinto the declarative region of the namespace are said to be member names of the namespace.
A namespacemember name has namespace scope. Its potential scope includes its namespace from the name’s point ofdeclaration (3.3.1) onwards; and for each using-directive (7.3.4) that nominates the member’s namespace,the member’s potential scope includes that portion of the potential scope of the using-directive that followsthe member’s point of declaration. [Example:namespace N {int i;int g(int a) { return a; }int j();void q();}namespace { int l=1; }// the potential scope of l is from its point of declaration// to the end of the translation unitnamespace N {int g(char a){return l+a;}// overloads N::g(int)// l is from unnamed namespaceint i;int j();// error: duplicate definition// OK: duplicate function declarationint j(){// OK: definition of N::j()return g(i);}int q();// calls N::g(int)// error: different return type}—end example]2A namespace member can also be referred to after the :: scope resolution operator (5.1) applied to thename of its namespace or the name of a namespace which nominates the member’s namespace in a usingdirective; see 3.4.3.2.3A name declared outside all named or unnamed namespaces (7.3), blocks (6.3), function declarations(8.3.5), function definitions (8.4) and classes (clause 9) has global namespace scope (also called globalscope).
The potential scope of such a name begins at its point of declaration (3.3.1) and ends at the end ofthe translation unit that is its declarative region. Names declared in the global namespace scope are said tobe global.3.3.6 Class scope1[basic.scope.class]The following rules describe the scope of names declared in classes.1) The potential scope of a name declared in a class consists not only of the declarative region followingthe name’s declarator, but also of all function bodies, default arguments, and constructor ctor-27ISO/IEC 14882:1998(E)© ISO/IEC3.3.6 Class scope3 Basic conceptsinitializers in that class (including such things in nested classes).2) A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in thecompleted scope of S.
No diagnostic is required for a violation of this rule.3) If reordering member declarations in a class yields an alternate valid program under (1) and (2), the program is ill-formed, no diagnostic is required.4) A name declared within a member function hides a declaration of the same name whose scope extendsto or past the end of the member function’s class.5) The potential scope of a declaration that extends to or past the end of a class definition also extends tothe regions defined by its member definitions, even if the members are defined lexically outside theclass (this includes static data member definitions, nested class definitions, member function definitions(including the member function body and, for constructor functions (12.1), the ctor-initializer (12.6.2))and any portion of the declarator part of such definitions which follows the identifier, including aparameter-declaration-clause and any default arguments (8.3.6).
[Example:typedef int c;enum { i = 1 };class X {char v[i];int f() { return sizeof(c); }char c;enum { i = 2 };// error: i refers to ::i// but when reevaluated is X::i// OK: X::c};typedef char*struct Y {T a;T;typedef longT b;// error: T refers to ::T// but when reevaluated is Y::TT;};typedef int I;class D {typedef I I;};// error, even though no reordering involved—end example]2The name of a class member shall only be used as follows:— in the scope of its class (as described above) or a class derived (clause 10) from its class,— after the .
operator applied to an expression of the type of its class (5.2.5) or a class derived from itsclass,— after the -> operator applied to a pointer to an object of its class (5.2.5) or a class derived from its class,— after the :: scope resolution operator (5.1) applied to the name of its class or a class derived from itsclass.3.3.7 Name hiding[basic.scope.hiding]1A name can be hidden by an explicit declaration of that same name in a nested declarative region or derivedclass (10.2).2A class name (9.1) or enumeration name (7.2) can be hidden by the name of an object, function, or enumerator declared in the same scope. If a class or enumeration name and an object, function, or enumerator are28© ISO/IEC3 Basic conceptsISO/IEC 14882:1998(E)3.3.7 Name hidingdeclared in the same scope (in any order) with the same name, the class or enumeration name is hiddenwherever the object, function, or enumerator name is visible.3In a member function definition, the declaration of a local name hides the declaration of a member of theclass with the same name; see 3.3.6.
The declaration of a member in a derived class (clause 10) hides thedeclaration of a member of a base class of the same name; see 10.2.4During the lookup of a name qualified by a namespace name, declarations that would otherwise be madevisible by a using-directive can be hidden by declarations with the same name in the namespace containingthe using-directive; see (3.4.3.2).5If a name is in scope and is not hidden it is said to be visible.3.4 Name lookup[basic.lookup]1The name lookup rules apply uniformly to all names (including typedef-names (7.1.3), namespace-names(7.3) and class-names (9.1)) wherever the grammar allows such names in the context discussed by a particular rule. Name lookup associates the use of a name with a declaration (3.1) of that name.
Name lookupshall find an unambiguous declaration for the name (see 10.2). Name lookup may associate more than onedeclaration with a name if it finds the name to be a function name; the declarations are said to form a set ofoverloaded functions (13.1). Overload resolution (13.3) takes place after name lookup has succeeded.
Theaccess rules (clause 11) are considered only once name lookup and function overload resolution (if applicable) have succeeded. Only after name lookup, function overload resolution (if applicable) and accesschecking have succeeded are the attributes introduced by the name’s declaration used further in expressionprocessing (clause 5).2A name “looked up in the context of an expression” is looked up as an unqualified name in the scope wherethe expression is found.3Because the name of a class is inserted in its class scope (clause 9), the name of a class is also considered amember of that class for the purposes of name hiding and lookup.4[Note: 3.5 discusses linkage issues.
The notions of scope, point of declaration and name hiding are discussed in 3.3. ]3.4.1 Unqualified name lookup[basic.lookup.unqual]1In all the cases listed in 3.4.1, the scopes are searched for a declaration in the order listed in each of therespective categories; name lookup ends as soon as a declaration is found for the name. If no declaration isfound, the program is ill-formed.2The declarations from the namespace nominated by a using-directive become visible in a namespaceenclosing the using-directive; see 7.3.4.
For the purpose of the unqualified name lookup rules described in3.4.1, the declarations from the namespace nominated by the using-directive are considered members ofthat enclosing namespace.3The lookup for an unqualified name used as the postfix-expression of a function call is described in 3.4.2.[Note: for purposes of determining (during parsing) whether an expression is a postfix-expression for afunction call, the usual name lookup rules apply. The rules in 3.4.2 have no effect on the syntactic interpretation of an expression.
For example,typedef int f;struct A {friend void f(A &);operator int();void g(A a) {f(a);}};The expression f(a) is a cast-expression equivalent to int(a). Because the expression is not a function29ISO/IEC 14882:1998(E)3.4.1 Unqualified name lookup© ISO/IEC3 Basic conceptscall, the argument-dependent name lookup (3.4.2) does not apply and the friend function f is not found. ]4A name used in global scope, outside of any function, class or user-declared namespace, shall be declaredbefore its use in global scope.5A name used in a user-declared namespace outside of the definition of any function or class shall bedeclared before its use in that namespace or before its use in a namespace enclosing its namespace.6A name used in the definition of a function26) that is a member of namespace N (where, only for the purpose of exposition, N could represent the global scope) shall be declared before its use in the block in whichit is used or in one of its enclosing blocks (6.3) or, shall be declared before its use in namespace N or, if N isa nested namespace, shall be declared before its use in one of N’s enclosing namespaces.[Example:namespace A {namespace N {void f();}}void A::N::f() {i = 5;// The following scopes are searched for a declaration of i:// 1) outermost block scope of A::N::f, before the use of i// 2) scope of namespace N// 3) scope of namespace A// 4) global scope, before the definition of A::N::f}—end example]7A name used in the definition of a class X outside of a member function body or nested class definition27)shall be declared in one of the following ways:— before its use in class X or be a member of a base class of X (10.2), or— if X is a nested class of class Y (9.7), before the definition of X in Y, or shall be a member of a base classof Y (this lookup applies in turn to Y’s enclosing classes, starting with the innermost enclosing class),28)or— if X is a local class (9.8) or is a nested class of a local class, before the definition of class X in a blockenclosing the definition of class X, or— if X is a member of namespace N, or is a nested class of a class that is a member of N, or is a local classor a nested class within a local class of a function that is a member of N, before the definition of class Xin namespace N or in one of N’s enclosing namespaces.[Example:namespace M {class B { };}__________________26) This refers to unqualified names following the function declarator; such a name may be used as a type or as a default argumentname in the parameter-declaration-clause, or may be used in the function body.27) This refers to unqualified names following the class name; such a name may be used in the base-clause or may be used in the classdefinition.28) This lookup applies whether the definition of X is nested within Y’s definition or whether X’s definition appears in a namespacescope enclosing Y’s definition (9.7).30© ISO/IEC3 Basic conceptsISO/IEC 14882:1998(E)3.4.1 Unqualified name lookupnamespace N {class Y : public M::B {class X {int a[i];};};}// The following scopes are searched for a declaration of i:// 1) scope of class N::Y::X, before the use of i// 2) scope of class N::Y, before the definition of N::Y::X// 3) scope of N::Y’s base class M::B// 4) scope of namespace N, before the definition of N::Y// 5) global scope, before the definition of N—end example] [Note: when looking for a prior declaration of a class or function introduced by a frienddeclaration, scopes outside of the innermost enclosing namespace scope are not considered; see 7.3.1.2.