Стандарт C++ 98 (1119566), страница 19
Текст из файла (страница 19)
] A standardconversion sequence will be applied to an expression if necessary to convert it to a required destinationtype.2[Note: expressions with a given type will be implicitly converted to other types in several contexts:— When used as operands of operators. The operator’s requirements for its operands dictate the destination type (clause 5).— When used in the condition of an if statement or iteration statement (6.4, 6.5). The destination type isbool.— When used in the expression of a switch statement. The destination type is integral (6.4).— When used as the source expression for an initialization (which includes use as an argument in a function call and use as the expression in a return statement).
The type of the entity being initialized is(generally) the destination type. See 8.5, 8.5.3.—end note]3An expression e can be implicitly converted to a type T if and only if the declaration “T t=e;” is wellformed, for some invented temporary variable t (8.5). The effect of the implicit conversion is the same asperforming the declaration and initialization and then using the temporary variable as the result of the conversion.
The result is an lvalue if T is a reference type (8.3.2), and an rvalue otherwise. The expression eis used as an lvalue if and only if the initialization uses it as an lvalue.4[Note: For user-defined types, user-defined conversions are considered as well; see 12.3. In general, animplicit conversion sequence (13.3.3.1) consists of a standard conversion sequence followed by a userdefined conversion followed by another standard conversion sequence.5There are some contexts where certain conversions are suppressed.
For example, the lvalue-to-rvalue conversion is not done on the operand of the unary & operator. Specific exceptions are given in the descriptions of those operators and contexts. ]4.1 Lvalue-to-rvalue conversion1[conv.lval]An lvalue (3.10) of a non-function, non-array type T can be converted to an rvalue. If T is an incompletetype, a program that necessitates this conversion is ill-formed. If the object to which the lvalue refers is notan object of type T and is not an object of a type derived from T, or if the object is uninitialized, a programthat necessitates this conversion has undefined behavior.
If T is a non-class type, the type of the rvalue isthe cv-unqualified version of T. Otherwise, the type of the rvalue is T. 49)__________________49) In C++ class rvalues can have cv-qualified types (because they are objects). This differs from ISO C, in which non-lvalues neverhave cv-qualified types.57ISO/IEC 14882:1998(E)4.1 Lvalue-to-rvalue conversion© ISO/IEC4 Standard conversions2The value contained in the object indicated by the lvalue is the rvalue result.
When an lvalue-to-rvalue conversion occurs within the operand of sizeof (5.3.3) the value contained in the referenced object is notaccessed, since that operator does not evaluate its operand.3[Note: See also 3.10. ]4.2 Array-to-pointer conversion[conv.array]1An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to an rvalueof type “pointer to T.” The result is a pointer to the first element of the array.2A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer tochar”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”.
In either case,the result is a pointer to the first element of the array. This conversion is considered only when there is anexplicit appropriate pointer target type, and not when there is a general need to convert from an lvalue to anrvalue. [Note: this conversion is deprecated. See Annex D. ] For the purpose of ranking in overload resolution (13.3.3.1.1), this conversion is considered an array-to-pointer conversion followed by a qualificationconversion (4.4). [Example: "abc" is converted to “pointer to const char” as an array-to-pointer conversion, and then to “pointer to char” as a qualification conversion. ]4.3 Function-to-pointer conversion[conv.func]1An lvalue of function type T can be converted to an rvalue of type “pointer to T.” The result is a pointer tothe function.50)2[Note: See 13.4 for additional rules for the case where the function is overloaded.
]4.4 Qualification conversions[conv.qual]1An rvalue of type “pointer to cv1 T” can be converted to an rvalue of type “pointer to cv2 T” if “cv2 T” ismore cv-qualified than “cv1 T.”2An rvalue of type “pointer to member of X of type cv1 T” can be converted to an rvalue of type “pointer tomember of X of type cv2 T” if “cv2 T” is more cv-qualified than “cv1 T.”3[Note: Function types (including those used in pointer to member function types) are never cv-qualified(8.3.5). ]4A conversion can add cv-qualifiers at levels other than the first in multi-level pointers, subject to the following rules:51)Two pointer types T1 and T2 are similar if there exists a type T and integer n > 0 such that:T1 is cv 1 , 0 pointer to cv 1 , 1 pointer to .
. . cv 1 ,n − 1 pointer to cv 1 ,n TandT2 is cv 2 , 0 pointer to cv 2 , 1 pointer to . . . cv 2 ,n − 1 pointer to cv 2 ,n Twhere each cv i, j is const, volatile, const volatile, or nothing. The n-tuple of cv-qualifiersafter the first in a pointer type, e.g., cv 1 , 1 , cv 1 , 2 , . . . , cv 1 ,n in the pointer type T1, is called the cvqualification signature of the pointer type.
An expression of type T1 can be converted to type T2 if andonly if the following conditions are satisfied:— the pointer types are similar.— for every j > 0, if const is in cv 1 , j then const is in cv 2 , j , and similarly for volatile.— if the cv 1 , j and cv 2 , j are different, then const is in every cv 2 ,k for 0 < k < j.__________________50) This conversion never applies to nonstatic member functions because an lvalue that refers to a nonstatic member function cannot beobtained.51) These rules ensure that const-safety is preserved by the conversion.58© ISO/IECISO/IEC 14882:1998(E)4 Standard conversions4.4 Qualification conversions[Note: if a program could assign a pointer of type T** to a pointer of type const T** (that is, if line //1below was allowed), a program could inadvertently modify a const object (as it is done on line //2). Forexample,int main() {const char c = ’c’;char* pc;const char** pcc = &pc;//1: not allowed*pcc = &c;*pc = ’C’;//2: modifies a const object}—end note]5A multi-level pointer to member type, or a multi-level mixed pointer and pointer to member type has theform:cv 0 P 0 to cv 1 P 1 to .
. . cv n − 1 P n − 1 to cv n Twhere P i is either a pointer or pointer to member and where T is not a pointer type or pointer to membertype.6Two multi-level pointer to member types or two multi-level mixed pointer and pointer to member types T1and T2 are similar if there exists a type T and integer n > 0 such that:T1 is cv 1 , 0 P 0 to cv 1 , 1 P 1 to . . . cv 1 ,n − 1 P n − 1 to cv 1 ,n TandT2 is cv 2 , 0 P 0 to cv 2 , 1 P 1 to . . . cv 2 ,n − 1 P n − 1 to cv 2 ,n T7For similar multi-level pointer to member types and similar multi-level mixed pointer and pointer to member types, the rules for adding cv-qualifiers are the same as those used for similar pointer types.4.5 Integral promotions[conv.prom]1An rvalue of type char, signed char, unsigned char, short int, or unsigned shortint can be converted to an rvalue of type int if int can represent all the values of the source type; otherwise, the source rvalue can be converted to an rvalue of type unsigned int.2An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) can be converted to an rvalue of the firstof the following types that can represent all the values of its underlying type: int, unsigned int,long, or unsigned long.3An rvalue for an integral bit-field (9.6) can be converted to an rvalue of type int if int can represent allthe values of the bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field.
If the bit-field is larger yet, no integral promotion applies to it. If thebit-field has an enumerated type, it is treated as any other value of that type for promotion purposes.4An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and truebecoming one.5These conversions are called integral promotions.4.6 Floating point promotion[conv.fpprom]1An rvalue of type float can be converted to an rvalue of type double.
The value is unchanged.2This conversion is called floating point promotion.59ISO/IEC 14882:1998(E)4.7 Integral conversions4.7 Integral conversions© ISO/IEC4 Standard conversions[conv.integral]1An rvalue of an integer type can be converted to an rvalue of another integer type. An rvalue of an enumeration type can be converted to an rvalue of an integer type.2If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the sourceinteger (modulo 2 n where n is the number of bits used to represent the unsigned type). [Note: In a two’scomplement representation, this conversion is conceptual and there is no change in the bit pattern (if thereis no truncation).
]3If the destination type is signed, the value is unchanged if it can be represented in the destination type (andbit-field width); otherwise, the value is implementation-defined.4If the destination type is bool, see 4.12. If the source type is bool, the value false is converted to zeroand the value true is converted to one.5The conversions allowed as integral promotions are excluded from the set of integral conversions.4.8 Floating point conversions[conv.double]1An rvalue of floating point type can be converted to an rvalue of another floating point type.