Стандарт C++ 98 (1119566), страница 33
Текст из файла (страница 33)
If,during parsing, a name in a template parameter is bound differently than it would be bound during a trialparse, the program is ill-formed. No diagnostic is required. [Note: This can occur only when the name isdeclared earlier in the declaration. ] [Example:struct T1 {T1 operator()(int x) { return T1(x); }int operator=(int x) { return x; }T1(int) { }};struct T2 { T2(int){ } };int a, (*(*b)(T2))(int), c, d;void f() {// disambiguation requires this to be parsed// as a declarationT1(a) = 3,T2(4),// T2 will be declared as(*(*b)(T2(c)))(int(d)); // a variable of type T1// but this will not allow// the last part of the// declaration to parse// properly since it depends// on T2 being a type-name}—end example]100© ISO/IECISO/IEC 14882:1998(E)7 Declarations1[dcl.dcl]Declarations specify how names are to be interpreted.
Declarations have the formdeclaration-seq:declarationdeclaration-seq declarationdeclaration:block-declarationfunction-definitiontemplate-declarationexplicit-instantiationexplicit-specializationlinkage-specificationnamespace-definitionblock-declaration:simple-declarationasm-definitionnamespace-alias-definitionusing-declarationusing-directivesimple-declaration:decl-specifier-seqopt init-declarator-listopt ;[Note: asm-definitions are described in 7.4, and linkage-specifications are described in 7.5. Functiondefinitions are described in 8.4 and template-declarations are described in clause 14.
Namespacedefinitions are described in 7.3.1, using-declarations are described in 7.3.3 and using-directives aredescribed in 7.3.4. ] The simple-declarationdecl-specifier-seqopt init-declarator-listopt ;is divided into two parts: decl-specifiers, the components of a decl-specifier-seq, are described in 7.1 anddeclarators, the components of an init-declarator-list, are described in clause 8.2A declaration occurs in a scope (3.3); the scope rules are summarized in 3.4. A declaration that declares afunction or defines a class, namespace, template, or function also has one or more scopes nested within it.These nested scopes, in turn, can have declarations nested within them. Unless otherwise stated, utterancesin clause 7 about components in, of, or contained by a declaration or subcomponent thereof refer only tothose components of the declaration that are not nested within scopes nested within the declaration.3In a simple-declaration, the optional init-declarator-list can be omitted only when declaring a class (clause9) or enumeration (7.2), that is, when the decl-specifier-seq contains either a class-specifier, an elaboratedtype-specifier with a class-key (9.1), or an enum-specifier.
In these cases and whenever a class-specifier orenum-specifier is present in the decl-specifier-seq, the identifiers in these specifiers are among the namesbeing declared by the declaration (as class-names, enum-names, or enumerators, depending on the syntax).In such cases, and except for the declaration of an unnamed bit-field (9.6), the decl-specifier-seq shall introduce one or more names into the program, or shall redeclare a name introduced by a previous declaration.[Example:enum { };typedef class { };// ill-formed// ill-formed—end example]101ISO/IEC 14882:1998(E)© ISO/IEC7 Declarations7 Declarations4Each init-declarator in the init-declarator-list contains exactly one declarator-id, which is the namedeclared by that init-declarator and hence one of the names declared by the declaration.
The type-specifiers(7.1.5) in the decl-specifier-seq and the recursive declarator structure of the init-declarator describe a type(8.3), which is then associated with the name being declared by the init-declarator.5If the decl-specifier-seq contains the typedef specifier, the declaration is called a typedef declaration andthe name of each init-declarator is declared to be a typedef-name, synonymous with its associated type(7.1.3). If the decl-specifier-seq contains no typedef specifier, the declaration is called a functiondeclaration if the type associated with the name is a function type (8.3.5) and an object declaration otherwise.6Syntactic components beyond those found in the general form of declaration are added to a function declaration to make a function-definition.
An object declaration, however, is also a definition unless it containsthe extern specifier and has no initializer (3.1). A definition causes the appropriate amount of storage tobe reserved and any appropriate initialization (8.5) to be done.7Only in function declarations for constructors, destructors, and type conversions can the decl-specifier-seqbe omitted.78)7.1 Specifiers1[dcl.spec]The specifiers that can be used in a declaration aredecl-specifier:storage-class-specifiertype-specifierfunction-specifierfriendtypedefdecl-specifier-seq:decl-specifier-seqopt decl-specifier2The longest sequence of decl-specifiers that could possibly be a type name is taken as the decl-specifier-seqof a declaration.
The sequence shall be self-consistent as described below. [Example:typedef char* Pc;static Pc;// error: name missingHere, the declaration static Pc is ill-formed because no name was specified for the static variable oftype Pc. To get a variable called Pc, a type-specifier (other than const or volatile) has to be presentto indicate that the typedef-name Pc is the name being (re)declared, rather than being part of the declspecifier sequence. For another example,void f(const Pc);void g(const int Pc);// void f(char* const) (not const char*)// void g(const int)—end example]3[Note: since signed, unsigned, long, and short by default imply int, a type-name appearing afterone of those specifiers is treated as the name being (re)declared.
[Example:void h(unsigned Pc);void k(unsigned int Pc);—end example] —end note]__________________78) The “implicit int” rule of C is no longer supported.102// void h(unsigned int)// void k(unsigned int)© ISO/IECISO/IEC 14882:1998(E)7 Declarations7.1 Specifiers7.1.1 Storage class specifiers1[dcl.stc]The storage class specifiers arestorage-class-specifier:autoregisterstaticexternmutableAt most one storage-class-specifier shall appear in a given decl-specifier-seq.
If a storage-class-specifierappears in a decl-specifier-seq, there can be no typedef specifier in the same decl-specifier-seq and theinit-declarator-list of the declaration shall not be empty (except for global anonymous unions, which shallbe declared static (9.5)). The storage-class-specifier applies to the name declared by each initdeclarator in the list and not to any names declared by other specifiers. A storage-class-specifier shall notbe specified in an explicit specialization (14.7.3) or an explicit instantiation (14.7.2) directive.2The auto or register specifiers can be applied only to names of objects declared in a block (6.3) or tofunction parameters (8.4). They specify that the named object has automatic storage duration (3.7.2).
Anobject declared without a storage-class-specifier at block scope or declared as a function parameter hasautomatic storage duration by default. [Note: hence, the auto specifier is almost always redundant and notoften used; one use of auto is to distinguish a declaration-statement from an expression-statement (6.8)explicitly. —end note]3A register specifier has the same semantics as an auto specifier together with a hint to the implementation that the object so declared will be heavily used. [Note: the hint can be ignored and in most implementations it will be ignored if the address of the object is taken.
—end note]4The static specifier can be applied only to names of objects and functions and to anonymous unions(9.5). There can be no static function declarations within a block, nor any static function parameters. A static specifier used in the declaration of an object declares the object to have static storageduration (3.7.1). A static specifier can be used in declarations of class members; 9.4 describes its effect.For the linkage of a name declared with a static specifier, see 3.5.5The extern specifier can be applied only to the names of objects and functions.
The extern specifiercannot be used in the declaration of class members or function parameters. For the linkage of a namedeclared with an extern specifier, see 3.5.6A name declared in a namespace scope without a storage-class-specifier has external linkage unless it hasinternal linkage because of a previous declaration and provided it is not declared const.
Objects declaredconst and not explicitly declared extern have internal linkage.7The linkages implied by successive declarations for a given entity shall agree. That is, within a givenscope, each declaration declaring the same object name or the same overloading of a function name shallimply the same linkage. Each function in a given set of overloaded functions can have a different linkage,however. [Example:static char* f();char* f(){ /* ... */ }// f() has internal linkage// f() still has internal linkagechar* g();static char* g(){ /* ... */ }// g() has external linkage// error: inconsistent linkagevoid h();inline void h();// external linkageinline void l();void l();// external linkage103ISO/IEC 14882:1998(E)© ISO/IEC7.1.1 Storage class specifiers7 Declarationsinline void m();extern void m();// external linkagestatic void n();inline void n();// internal linkagestatic int a;int a;// a has internal linkage// error: two definitionsstatic int b;extern int b;// b has internal linkage// b still has internal linkageint c;static int c;// c has external linkage// error: inconsistent linkageextern int d;static int d;// d has external linkage// error: inconsistent linkage—end example]8The name of a declared but undefined class can be used in an extern declaration.
Such a declaration canonly be used in ways that do not require a complete class type. [Example:structexternexternexternS;S a;S f();void g(S);void h(){g(a);f();}// error: S is incomplete// error: S is incomplete—end example] The mutable specifier can be applied only to names of class data members (9.2) andcannot be applied to names declared const or static, and cannot be applied to reference members.[Example:class X {mutable const int* p;mutable int* const q;};// OK// ill-formed—end example]9The mutable specifier on a class data member nullifies a const specifier applied to the containing classobject and permits modification of the mutable class member even though the rest of the object is const(7.1.5.1).7.1.2 Function specifiers1[dcl.fct.spec]Function-specifiers can be used only in function declarations.function-specifier:inlinevirtualexplicit2A function declaration (8.3.5, 9.3, 11.4) with an inline specifier declares an inline function.