Стандарт C++ 98 (1119566), страница 25
Текст из файла (страница 25)
. . 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 T is any object type or the void type and where cv 1 ,k and cv 2 ,k may be different cv-qualifications,an rvalue of type T1 may be explicitly converted to the type T2 using a const_cast. The result of apointer const_cast refers to the original object.4An lvalue of type T1 can be explicitly converted to an lvalue of type T2 using the castconst_cast<T2&> (where T1 and T2 are object types) if a pointer to T1 can be explicitly converted tothe type pointer to T2 using a const_cast. The result of a reference const_cast refers to the original object.__________________65) The types may have different cv-qualifiers, subject to the overall restriction that a reinterpret_cast cannot cast away constness.66) T1 and T2 may have different cv-qualifiers, subject to the overall restriction that a reinterpret_cast cannot cast away constness.67) This is sometimes referred to as a type pun.74© ISO/IECISO/IEC 14882:1998(E)5 Expressions5.2.11 Const cast5For a const_cast involving pointers to data members, multi-level pointers to data members and multilevel mixed pointers and pointers to data members (4.4), the rules for const_cast are the same as thoseused for pointers; the “member” aspect of a pointer to member is ignored when determining where the cvqualifiers are added or removed by the const_cast.
The result of a pointer to data memberconst_cast refers to the same member as the original (uncast) pointer to data member.6A null pointer value (4.10) is converted to the null pointer value of the destination type. The null memberpointer value (4.11) is converted to the null member pointer value of the destination type.7[Note: Depending on the type of the object, a write operation through the pointer, lvalue or pointer to datamember resulting from a const_cast that casts away a const-qualifier68) may produce undefined behavior (7.1.5.1). ]8The following rules define the process known as casting away constness.
In these rules Tn and Xn represent types. For two pointer types:X 1 is T 1cv 1 , 1 * . . . cv 1 ,N *where T 1 is not a pointer type* . . . cv 2 ,M *where T 2 is not a pointer typeX 2 is T 2cv 2 , 1K is min(N,M)casting from X1 to X2 casts away constness if, for a non-pointer type T there does not exist an implicit conversion (clause 4) from:Tcv 1 , (N − K + 1 ) * cv 1 , (N − K + 2 ) * . .
. cv 1 ,N *toTcv 2 , (M − K + 1 ) * cv 2 , (M − K + 2 ) * . . . cv 2 ,M *9Casting from an lvalue of type T1 to an lvalue of type T2 using a reference cast casts away constness if acast from an rvalue of type “pointer to T1” to the type “pointer to T2” casts away constness.10Casting from an rvalue of type “pointer to data member of X of type T1” to the type “pointer to data member of Y of type T2” casts away constness if a cast from an rvalue of type “pointer to T1” to the type“pointer to T2” casts away constness.11For multi-level pointer to members and multi-level mixed pointers and pointer to members (4.4), the“member” aspect of a pointer to member level is ignored when determining if a const cv-qualifier hasbeen cast away.12[Note: some conversions which involve only changes in cv-qualification cannot be done usingconst_cast.
For instance, conversions between pointers to functions are not covered because suchconversions lead to values whose use causes undefined behavior. For the same reasons, conversionsbetween pointers to member functions, and in particular, the conversion from a pointer to a const memberfunction to a pointer to a non-const member function, are not covered. ]__________________68) const_cast is not limited to conversions that cast away a const-qualifier.75ISO/IEC 14882:1998(E)1© ISO/IEC5.2.11 Const cast5 Expressions5.3 Unary expressions[expr.unary]Expressions with unary operators group right-to-left.unary-expression:postfix-expression++ cast-expression-- cast-expressionunary-operator cast-expressionsizeof unary-expressionsizeof ( type-id )new-expressiondelete-expressionunary-operator: one of* & + -!~5.3.1 Unary operators[expr.unary.op]1The unary * operator performs indirection: the expression to which it is applied shall be a pointer to anobject type, or a pointer to a function type and the result is an lvalue referring to the object or function towhich the expression points.
If the type of the expression is “pointer to T,” the type of the result is “T.”[Note: a pointer to an incomplete type (other than cv void ) can be dereferenced. The lvalue thus obtainedcan be used in limited ways (to initialize a reference, for example); this lvalue must not be converted to anrvalue, see 4.1. ]2The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualifiedid.
In the first case, if the type of the expression is “T,” the type of the result is “pointer to T.” In particular,the address of an object of type “cv T” is “pointer to cv T,” with the same cv-qualifiers. For a qualified-id,if the member is a static member of type “T”, the type of the result is plain “pointer to T.” If the member isa nonstatic member of class C of type T, the type of the result is “pointer to member of class C of typeT.” [Example:struct A { int i; };struct B : A { };... &B::i ...// has type int A::*—end example] [Note: a pointer to member formed from a mutable nonstatic data member (7.1.1) doesnot reflect the mutable specifier associated with the nonstatic data member.
]3A pointer to member is only formed when an explicit & is used and its operand is a qualified-id notenclosed in parentheses. [Note: that is, the expression &(qualified-id), where the qualified-id isenclosed in parentheses, does not form an expression of type “pointer to member.” Neither doesqualified-id, because there is no implicit conversion from a qualified-id for a nonstatic member function to the type “pointer to member function” as there is from an lvalue of function type to the type “pointerto function” (4.3).
Nor is &unqualified-id a pointer to member, even within the scope of theunqualified-id’s class. ]4The address of an object of incomplete type can be taken, but if the complete type of that object is a classtype that declares operator&() as a member function, then the behavior is undefined (and no diagnosticis required). The operand of & shall not be a bit-field.5The address of an overloaded function (clause 13) can be taken only in a context that uniquely determineswhich version of the overloaded function is referred to (see 13.4). [Note: since the context might determinewhether the operand is a static or nonstatic member function, the context can also affect whether the expression has type “pointer to function” or “pointer to member function.” ]6The operand of the unary + operator shall have arithmetic, enumeration, or pointer type and the result is thevalue of the argument.
Integral promotion is performed on integral or enumeration operands. The type ofthe result is the type of the promoted operand.76© ISO/IEC5 ExpressionsISO/IEC 14882:1998(E)5.3.1 Unary operators7The operand of the unary - operator shall have arithmetic or enumeration type and the result is the negationof its operand.
Integral promotion is performed on integral or enumeration operands. The negative of anunsigned quantity is computed by subtracting its value from 2 n , where n is the number of bits in the promoted operand. The type of the result is the type of the promoted operand.8The operand of the logical negation operator ! is implicitly converted to bool (clause 4); its value istrue if the converted operand is false and false otherwise. The type of the result is bool.9The operand of ~ shall have integral or enumeration type; the result is the one’s complement of its operand.Integral promotions are performed.
The type of the result is the type of the promoted operand. There is anambiguity in the unary-expression ~X(), where X is a class-name. The ambiguity is resolved in favor oftreating ~ as a unary complement rather than treating ~X as referring to a destructor.5.3.2 Increment and decrement[expr.pre.incr]1The operand of prefix ++ is modified by adding 1, or set to true if it is bool (this use is deprecated).The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointerto a completely-defined object type.
The value is the new value of the operand; it is an lvalue. If x is notof type bool, the expression ++x is equivalent to x+=1. [Note: see the discussions of addition (5.7) andassignment operators (5.17) for information on conversions. ]2The operand of prefix -- is modified by subtracting 1. The operand shall not be of type bool. Therequirements on the operand of prefix -- and the properties of its result are otherwise the same as those ofprefix ++.
[Note: For postfix increment and decrement, see 5.2.6. ]5.3.3 Sizeof[expr.sizeof]1The sizeof operator yields the number of bytes in the object representation of its operand. The operandis either an expression, which is not evaluated, or a parenthesized type-id. The sizeof operator shall notbe applied to an expression that has function or incomplete type, or to an enumeration type before all itsenumerators have been declared, or to the parenthesized name of such types, or to an lvalue that designatesa bit-field.