Стандарт C++ 98 (1119566), страница 41
Текст из файла (страница 41)
]8.3.5 Functions1[dcl.fct]In a declaration T D where D has the formD1 ( parameter-declaration-clause ) cv-qualifier-seqopt exception-specificationoptand the type of the contained declarator-id in the declaration T D1 is “derived-declarator-type-list T,” thetype of the declarator-id in D is “derived-declarator-type-list function of (parameter-declaration-clause)cv-qualifier-seqopt returning T”; a type of this form is a function type86).parameter-declaration-clause:parameter-declaration-listopt ...optparameter-declaration-list , ...parameter-declaration-list:parameter-declarationparameter-declaration-list , parameter-declarationparameter-declaration:decl-specifier-seqdecl-specifier-seqdecl-specifier-seqdecl-specifier-seq2declaratordeclarator = assignment-expressionabstract-declaratoroptabstract-declaratoropt = assignment-expressionThe parameter-declaration-clause determines the arguments that can be specified, and their processing,when the function is called.
[Note: the parameter-declaration-clause is used to convert the argumentsspecified on the function call; see 5.2.2. ] If the parameter-declaration-clause is empty, the function takesno arguments. The parameter list (void) is equivalent to the empty parameter list. Except for this special case, void shall not be a parameter type (though types derived from void, such as void*, can). Ifthe parameter-declaration-clause terminates with an ellipsis, the number of arguments shall be equal to orgreater than the number of parameters specified.
Where syntactically correct, “, ...” is synonymouswith “...”. [Example: the declarationint printf(const char*, ...);declares a function that can be called with varying numbers and types of arguments.printf("hello world");printf("a=%d b=%d", a, b);However, the first argument must be of a type that can be converted to a const char*. ] [Note: the standard header <cstdarg> contains a mechanism for accessing arguments passed using the ellipsis (see5.2.2 and 18.7). ]3A single name can be used for several different functions in a single scope; this is function overloading(clause 13). All declarations for a function with a given parameter list shall agree exactly both in the typeof the value returned and in the number and type of parameters; the presence or absence of the ellipsis isconsidered part of the function type.
The type of a function is determined using the following rules. Thetype of each parameter is determined from its own decl-specifier-seq and declarator. After determining thetype of each parameter, any parameter of type “array of T” or “function returning T” is adjusted to be“pointer to T” or “pointer to function returning T,” respectively. After producing the list of parametertypes, several transformations take place upon these types to determine the function type.
Any cv-qualifiermodifying a parameter type is deleted. [Example: the type void(*)(const int) becomesvoid(*)(int) —end example] Such cv-qualifiers affect only the definition of the parameter within the__________________86) As indicated by the syntax, cv-qualifiers are a significant component in function return types.135ISO/IEC 14882:1998(E)© ISO/IEC8.3.5 Functions8 Declaratorsbody of the function; they do not affect the function type.
If a storage-class-specifier modifies a parametertype, the specifier is deleted. [Example: register char* becomes char* —end example] Suchstorage-class-specifiers affect only the definition of the parameter within the body of the function; they donot affect the function type. The resulting list of transformed parameter types is the function’s parametertype list.4A cv-qualifier-seq shall only be part of the function type for a nonstatic member function, the function typeto which a pointer to member refers, or the top-level function type of a function typedef declaration.
Theeffect of a cv-qualifier-seq in a function declarator is not the same as adding cv-qualification on top of thefunction type, i.e., it does not create a cv-qualified function type. In fact, if at any time in the determinationof a type a cv-qualified function type is formed, the program is ill-formed. [Example:typedef void F();struct S {const F f;// ill-formed:// not equivalent to: void f() const;};—end example] The return type, the parameter type list and the cv-qualifier-seq, but not the default arguments (8.3.6) or the exception specification (15.4), are part of the function type.
[Note: function types arechecked during the assignments and initializations of pointer-to-functions, reference-to-functions, andpointer-to-member-functions. ]5[Example: the declarationint fseek(FILE*, long, int);declares a function taking three arguments of the specified types, and returning int (7.1.5). ]6If the type of a parameter includes a type of the form “pointer to array of unknown bound of T” or “reference to array of unknown bound of T,” the program is ill-formed.87) Functions shall not have a return typeof type array or function, although they may have a return type of type pointer or reference to such things.There shall be no arrays of functions, although there can be arrays of pointers to functions.
Types shall notbe defined in return or parameter types. The type of a parameter or the return type for a function declaration that is not a definition may be an incomplete class type.7A typedef of function type may be used to declare a function but shall not be used to define a function (8.4).[Example:typedef void F();F fv;F fv { }void fv() { }// OK: equivalent to void fv();// ill-formed// OK: definition of fv—end example] A typedef of a function type whose declarator includes a cv-qualifier-seq shall be usedonly to declare the function type for a nonstatic member function, to declare the function type to which apointer to member refers, or to declare the top-level function type of another function typedef declaration.[Example:typedef int FIC(int) const;FIC f;struct S {FIC f;};FIC S::*pm = &S::f;// ill-formed: does not declare a member function// OK// OK—end example]__________________87) This excludes parameters of type “ptr-arr-seq T2” where T2 is “pointer to array of unknown bound of T” and where ptr-arr-seqmeans any sequence of “pointer to” and “array of” derived declarator types.
This exclusion applies to the parameters of the function,and if a parameter is a pointer to function or pointer to member function then to its parameters also, etc.136© ISO/IECISO/IEC 14882:1998(E)8 Declarators8.3.5 Functions8An identifier can optionally be provided as a parameter name; if present in a function definition (8.4), itnames a parameter (sometimes called “formal argument”). [Note: in particular, parameter names are alsooptional in function definitions and names used for a parameter in different declarations and the definitionof a function need not be the same. If a parameter name is present in a function declaration that is not adefinition, it cannot be used outside of the parameter-declaration-clause since it goes out of scope at theend of the function declarator (3.3). ]9[Example: the declarationint i,*pi,f(),*fpi(int),(*pif)(const char*, const char*);(*fpif(int))(int);declares an integer i, a pointer pi to an integer, a function f taking no arguments and returning an integer,a function fpi taking an integer argument and returning a pointer to an integer, a pointer pif to a functionwhich takes two pointers to constant characters and returns an integer, a function fpif taking an integerargument and returning a pointer to a function that takes an integer argument and returns an integer.
It isespecially useful to compare fpi and pif. The binding of *fpi(int) is *(fpi(int)), so the declaration suggests, and the same construction in an expression requires, the calling of a function fpi, and thenusing indirection through the (pointer) result to yield an integer. In the declarator (*pif)(constchar*, const char*), the extra parentheses are necessary to indicate that indirection through a pointerto a function yields a function, which is then called.
] [Note: typedefs are sometimes convenient when thereturn type of a function is complex. For example, the function fpif above could have been declaredtypedef int IFUNC(int);IFUNC* fpif(int);—end note]8.3.6 Default arguments[dcl.fct.default]1If an expression is specified in a parameter declaration this expression is used as a default argument.Default arguments will be used in calls where trailing arguments are missing.2[Example: the declarationvoid point(int = 3, int = 4);declares a function that can be called with zero, one, or two arguments of type int. It can be called in anyof these ways:point(1,2);point(1);point();The last two calls are equivalent to point(1,4) and point(3,4), respectively.
]3A default argument expression shall be specified only in the parameter-declaration-clause of a functiondeclaration or in a template-parameter (14.1). If it is specified in a parameter-declaration-clause, it shallnot occur within a declarator or abstract-declarator of a parameter-declaration.88)4For non-template functions, default arguments can be added in later declarations of a function in the samescope. Declarations in different scopes have completely distinct sets of default arguments. That is, declarations in inner scopes do not acquire default arguments from declarations in outer scopes, and vice versa. Ina given function declaration, all parameters subsequent to a parameter with a default argument shall havedefault arguments supplied in this or previous declarations.
A default argument shall not be redefined by alater declaration (not even to the same value). [Example:__________________88) This means that default arguments cannot appear, for example, in declarations of pointers to functions, references to functions, ortypedef declarations.137ISO/IEC 14882:1998(E)© ISO/IEC8.3.6 Default argumentsvoid f(int, int);void f(int, int = 7);void h(){f(3);void f(int = 1, int);8 Declarators// OK, calls f(3, 7)// error: does not use default// from surrounding scope}void m(){void f(int, int);f(4);void f(int, int = 5);f(4);void f(int, int = 5);}void n(){f(6);}// has no defaults// error: wrong number of arguments// OK// OK, calls f(4, 5);// error: cannot redefine, even to// same value// OK, calls f(6, 7)—end example] For a given inline function defined in different translation units, the accumulated sets ofdefault arguments at the end of the translation units shall be the same; see 3.2.5A default argument expression is implicitly converted (clause 4) to the parameter type.