Стандарт C++ 98 (1119566), страница 42
Текст из файла (страница 42)
The default argument expression has the same semantic constraints as the initializer expression in a declaration of a variableof the parameter type, using the copy-initialization semantics (8.5). The names in the expression are bound,and the semantic constraints are checked, at the point where the default argument expression appears.Name lookup and checking of semantic constraints for default arguments in function templates and inmember functions of class templates are performed as described in 14.7.1. [Example: in the followingcode, g will be called with the value f(1):int a = 1;int f(int);int g(int x = f(a));void h() {a = 2;{int a = 3;g();}}// default argument: f(::a)// g(f(::a))—end example] [Note: in member function declarations, names in default argument expressions are lookedup as described in 3.4.1.
Access checking applies to names in default argument expressions as described inclause 11. ]6The default arguments in a member function definition that appears outside of the class definition are addedto the set of default arguments provided by the member function declaration in the class definition. [Example:class C {void f(int i = 3);void g(int i, int j = 99);};138© ISO/IECISO/IEC 14882:1998(E)8 Declaratorsvoid C::f(int i = 3){ }void C::g(int i = 88, int j){ }8.3.6 Default arguments// error: default argument already// specified in class scope// in this translation unit,// C::g can be called with no argument—end example]7Local variables shall not be used in default argument expressions. [Example:void f(){int i;extern void g(int x = i);// ...}// error—end example]8The keyword this shall not be used in a default argument of a member function.
[Example:class A {void f(A* p = this) { }};// error—end example]9Default arguments are evaluated each time the function is called. The order of evaluation of function arguments is unspecified. Consequently, parameters of a function shall not be used in default argument expressions, even if they are not evaluated. Parameters of a function declared before a default argument expression are in scope and can hide namespace and class member names. [Example:int a;int f(int a, int b = a);// error: parameter a// used as default argumenttypedef int I;int g(float I, int b = I(2));int h(int a, int b = sizeof(a));// error: parameter I found// error, parameter a used// in default argument—end example] Similarly, a nonstatic member shall not be used in a default argument expression, even if itis not evaluated, unless it appears as the id-expression of a class member access expression (5.2.5) or unlessit is used to form a pointer to member (5.3.1). [Example: the declaration of X::mem1() in the followingexample is ill-formed because no object is supplied for the nonstatic member X::a used as an initializer.int b;class X {int a;int mem1(int i = a);int mem2(int i = b);static int b;// error: nonstatic member a// used as default argument// OK; use X::b};The declaration of X::mem2() is meaningful, however, since no object is needed to access the staticmember X::b.
Classes, objects, and members are described in clause 9. ] A default argument is not partof the type of a function. [Example:139ISO/IEC 14882:1998(E)© ISO/IEC8.3.6 Default arguments8 Declaratorsint f(int = 0);void h(){int j = f(1);int k = f();}int (*p1)(int) = &f;int (*p2)() = &f;// OK, means f(0)// error: type mismatch—end example] When a declaration of a function is introduced by way of a using-declaration (7.3.3), anydefault argument information associated with the declaration is made known as well. If the function isredeclared thereafter in the namespace with additional default arguments, the additional arguments are alsoknown at any point following the redeclaration where the using-declaration is in scope.10A virtual function call (10.3) uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derivedclass does not acquire default arguments from the function it overrides.
[Example:struct A {virtual void f(int a = 7);};struct B : public A {void f(int a);};void m(){B* pb = new B;A* pa = pb;pa->f();pb->f();}// OK, calls pa->B::f(7)// error: wrong number of arguments for B::f()—end example]8.4 Function definitions1[dcl.fct.def]Function definitions have the formfunction-definition:decl-specifier-seqopt declarator ctor-initializeropt function-bodydecl-specifier-seqopt declarator function-try-blockfunction-body:compound-statementThe declarator in a function-definition shall have the formD1 ( parameter-declaration-clause ) cv-qualifier-seqopt exception-specificationoptas described in 8.3.5.
A function shall be defined only in namespace or class scope.2[Example: a simple example of a complete function definition isint max(int a, int b, int c){int m = (a > b) ? a : b;return (m > c) ? m : c;}Here int is the decl-specifier-seq; max(int a, int b, int c) is the declarator; { /* ... */ } isthe function-body. ]140© ISO/IECISO/IEC 14882:1998(E)8 Declarators8.4 Function definitions3A ctor-initializer is used only in a constructor; see 12.1 and 12.6.4A cv-qualifier-seq can be part of a non-static member function declaration, non-static member function definition, or pointer to member function only; see 9.3.2. It is part of the function type.5[Note: unused parameters need not be named. For example,void print(int a, int){printf("a = %d\n",a);}—end note]8.5 Initializers1[dcl.init]A declarator can specify an initial value for the identifier being declared.
The identifier designates anobject or reference being initialized. The process of initialization described in the remainder of 8.5 appliesalso to initializations specified by other syntactic contexts, such as the initialization of function parameterswith argument expressions (5.2.2) or the initialization of return values (6.6.3).initializer:= initializer-clause( expression-list )initializer-clause:assignment-expression{ initializer-list ,opt }{ }initializer-list:initializer-clauseinitializer-list , initializer-clause2Automatic, register, static, and external variables of namespace scope can be initialized by arbitrary expressions involving literals and previously declared variables and functions.
[Example:intintintintf(int);a = 2;b = f(a);c(b);—end example]3[Note: default argument expressions are more restricted; see 8.3.6.4The order of initialization of static objects is described in 3.6 and 6.7. ]5To zero-initialize storage for an object of type T means:— if T is a scalar type (3.9), the storage is set to the value of 0 (zero) converted to T;— if T is a non-union class type, the storage for each nonstatic data member and each base-class subobjectis zero-initialized;— if T is a union type, the storage for its first data member89) is zero-initialized;— if T is an array type, the storage for each element is zero-initialized;— if T is a reference type, no initialization is performed.__________________89) This member must not be static, by virtue of the requirements in 9.5.141ISO/IEC 14882:1998(E)© ISO/IEC8.5 Initializers8 DeclaratorsTo default-initialize an object of type T means:— if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization isill-formed if T has no accessible default constructor);— if T is an array type, each element is default-initialized;— otherwise, the storage for the object is zero-initialized.A program that calls for default-initialization of an entity of reference type is ill-formed.
If T is a cvqualified type, the cv-unqualified version of T is used for these definitions of zero-initialization anddefault-initialization.6The memory occupied by any object of static storage duration shall be zero-initialized at program startupbefore any other initialization takes place.
[Note: in some cases, additional initialization is done later. ]7An object whose initializer is an empty set of parentheses, i.e., (), shall be default-initialized.8[Note: since () is not permitted by the syntax for initializer,X a();is not the declaration of an object of class X, but the declaration of a function taking no argument andreturning an X. The form () is permitted in certain other initialization contexts (5.3.4, 5.2.3, 12.6.2). ]9If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (orarray thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlyingclass type shall have a user-declared default constructor.
Otherwise, if no initializer is specified for anobject, the object and its subobjects, if any, have an indeterminate initial value90); if the object or any of itssubobjects are of const-qualified type, the program is ill-formed.10An initializer for a static member is in the scope of the member’s class. [Example:int a;struct X {static int a;static int b;};int X::a = 1;int X::b = a;// X::b = X::a—end example]11The form of initialization (using parentheses or =) is generally insignificant, but does matter when theentity being initialized has a class type; see below.
A parenthesized initializer can be a list of expressionsonly when the entity being initialized has a class type.12The initialization that occurs in argument passing, function return, throwing an exception (15.1), handlingan exception (15.3), and brace-enclosed initializer lists (8.5.1) is called copy-initialization and is equivalentto the formT x = a;The initialization that occurs in new expressions (5.3.4), static_cast expressions (5.2.9), functionalnotation type conversions (5.2.3), and base and member initializers (12.6.2) is called direct-initializationand is equivalent to the formT x(a);__________________90) This does not apply to aggregate objects with automatic storage duration initialized with an incomplete brace-enclosed initializerlist; see 8.5.1.142© ISO/IECISO/IEC 14882:1998(E)8 Declarators138.5 InitializersIf T is a scalar type, then a declaration of the formT x = { a };is equivalent toT x = a;14The semantics of initializers are as follows.
The destination type is the type of the object or reference beinginitialized and the source type is the type of the initializer expression. The source type is not defined whenthe initializer is brace-enclosed or when it is a parenthesized list of expressions.— If the destination type is a reference type, see 8.5.3.— If the destination type is an array of characters or an array of wchar_t, and the initializer is a string literal, see 8.5.2.— Otherwise, if the destination type is an array, see 8.5.1.— If the destination type is a (possibly cv-qualified) class type:— If the class is an aggregate (8.5.1), and the initializer is a brace-enclosed list, see 8.5.1.— If the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, constructors are considered.