Стандарт C++ 11 (1119564), страница 36
Текст из файла (страница 36)
The effect of callinga function through a pointer to a function type (8.3.5) that is not the same as the type used in the definitionof the function is undefined. Except that converting a prvalue of type “pointer to T1” to the type “pointer toT2” (where T1 and T2 are function types) and back to its original type yields the original pointer value, theresult of such a pointer conversion is unspecified. [ Note: see also 4.10 for more details of pointer conversions.— end note ]7An object pointer can be explicitly converted to an object pointer of a different type.70 When a prvaluev of type “pointer to T1” is converted to the type “pointer to cv T2”, the result is static_cast<cvT2*>(static_cast<cv void*>(v)) if both T1 and T2 are standard-layout types (3.9) and the alignmentrequirements of T2 are no stricter than those of T1, or if either type is void.
Converting a prvalue of type“pointer to T1” to the type “pointer to T2” (where T1 and T2 are object types and where the alignmentrequirements of T2 are no stricter than those of T1) and back to its original type yields the original pointervalue. The result of any other such pointer conversion is unspecified.8Converting a function pointer to an object pointer type or vice versa is conditionally-supported. The meaningof such a conversion is implementation-defined, except that if an implementation supports conversions inboth directions, converting a prvalue of one type to the other type and back, possibly with different cvqualification, shall yield the original pointer value.9The null pointer value (4.10) is converted to the null pointer value of the destination type.
[ Note: A nullpointer constant of type std::nullptr_t cannot be converted to a pointer type, and a null pointer constantof integral type is not necessarily converted to a null pointer value. — end note ]10A prvalue of type “pointer to member of X of type T1” can be explicitly converted to a prvalue of a differenttype “pointer to member of Y of type T2” if T1 and T2 are both function types or both object types.71 Thenull member pointer value (4.11) is converted to the null member pointer value of the destination type. Theresult of this conversion is unspecified, except in the following cases:— converting a prvalue of type “pointer to member function” to a different pointer to member functiontype and back to its original type yields the original pointer to member value.— converting a prvalue of type “pointer to data member of X of type T1” to the type “pointer to datamember of Y of type T2” (where the alignment requirements of T2 are no stricter than those of T1)and back to its original type yields the original pointer to member value.11An lvalue expression of type T1 can be cast to the type “reference to T2” if an expression of type “pointer toT1” can be explicitly converted to the type “pointer to T2” using a reinterpret_cast.
That is, a referencecast reinterpret_cast<T&>(x) has the same effect as the conversion *reinterpret_cast<T*>(&x) withthe built-in & and * operators (and similarly for reinterpret_cast<T&&>(x)). The result refers to the sameobject as the source lvalue, but with a different type. The result is an lvalue for an lvalue reference type oran rvalue reference to function type and an xvalue for an rvalue reference to object type.
No temporary iscreated, no copy is made, and constructors (12.1) or conversion functions (12.3) are not called.725.2.111Const cast[expr.const.cast]The result of the expression const_cast<T>(v) is of type T. If T is an lvalue reference to object type, theresult is an lvalue; if T is an rvalue reference to object type, the result is an xvalue; otherwise, the resultis a prvalue and the lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard70) The types may have different cv-qualifiers, subject to the overall restriction that a reinterpret_cast cannot cast awayconstness.71) T1 and T2 may have different cv-qualifiers, subject to the overall restriction that a reinterpret_cast cannot cast awayconstness.72) This is sometimes referred to as a type pun.§ 5.2.11© ISO/IEC 2011 – All rights reserved107ISO/IEC 14882:2011(E)conversions are performed on the expression v.
Conversions that can be performed explicitly using const_cast are listed below. No other conversion shall be performed explicitly using const_cast.2[ Note: Subject to the restrictions in this section, an expression may be cast to its own type using a const_cast operator. — end note ]3For two pointer types T1 and T2 whereT1 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 T is any object type or the void type and where cv 1,k and cv 2,k may be different cv-qualifications, aprvalue of type T1 may be explicitly converted to the type T2 using a const_cast.
The result of a pointerconst_cast refers to the original object.4For two object types T1 and T2, if a pointer to T1 can be explicitly converted to the type “pointer to T2”using a const_cast, then the following conversions can also be made:— an lvalue of type T1 can be explicitly converted to an lvalue of type T2 using the cast const_cast<T2&>;— a glvalue of type T1 can be explicitly converted to an xvalue of type T2 using the cast const_cast<T2&&>; and— if T1 is a class type, a prvalue of type T1 can be explicitly converted to an xvalue of type T2 using thecast const_cast<T2&&>.The result of a reference const_cast refers to the original object.5For a const_cast involving pointers to data members, multi-level pointers to data members and multi-levelmixed pointers and pointers to data members (4.4), the rules for const_cast are the same as those used forpointers; the “member” aspect of a pointer to member is ignored when determining where the cv-qualifiersare added or removed by the const_cast.
The result of a pointer to data member const_cast refers to thesame 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 pointerto data member resulting from a const_cast that casts away a const-qualifier73 may produce undefinedbehavior (7.1.6.1). — end note ]8The following rules define the process known as casting away constness. In these rules Tn and Xn representtypes.
For two pointer types:X1 is T1cv 1,1 * · · · cv 1,N * where T1 is not a pointer typeX2 is T2cv 2,1 * · · · cv 2,M * where T2 is not a pointer typeK is min(N, M )casting from X1 to X2 casts away constness if, for a non-pointer type T there does not exist an implicitconversion (Clause 4) from:Tcv 1,(N −K+1) * cv 1,(N −K+2) * · · · cv 1,N *to73) const_cast is not limited to conversions that cast away a const-qualifier.§ 5.2.11108© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)Tcv 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 an lvalue reference cast or casting from anexpression of type T1 to an xvalue of type T2 using an rvalue reference cast casts away constness if a castfrom a prvalue of type “pointer to T1” to the type “pointer to T2” casts away constness.10Casting from a prvalue of type “pointer to data member of X of type T1” to the type “pointer to data memberof Y of type T2” casts away constness if a cast from a prvalue of type “pointer to T1” to the type “pointerto 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 has been castaway.12[ Note: some conversions which involve only changes in cv-qualification cannot be done using const_cast.For instance, conversions between pointers to functions are not covered because such conversions lead tovalues whose use causes undefined behavior.
For the same reasons, conversions between pointers to memberfunctions, and in particular, the conversion from a pointer to a const member function to a pointer to anon-const member function, are not covered. — end note ]5.31Unary 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 )sizeof ... ( identifier )alignof ( type-id )noexcept-expressionnew-expressiondelete-expressionunary-operator: one of* & + - ! ~5.3.1Unary 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 functionto which 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 aprvalue, see 4.1. — end note ]2The result of each of the following unary operators is a prvalue.3The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualifiedid. If the operand is a qualified-id naming a non-static member m of some class C with type T, the result hastype “pointer to member of class C of type T” and is a prvalue designating C::m. Otherwise, if the type ofthe expression is T, the result has type “pointer to T” and is a prvalue that is the address of the designated§ 5.3.1© ISO/IEC 2011 – All rights reserved109ISO/IEC 14882:2011(E)object (1.7) or a pointer to the designated function.
[ Note: In particular, the address of an object of type“cv T” is “pointer to cv T”, with the same cv-qualification. — end note ] [ 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 non-static data member (7.1.1) doesnot reflect the mutable specifier associated with the non-static data member. — end note ]4A pointer to member is only formed when an explicit & is used and its operand is a qualified-id not enclosedin parentheses.