[expr.reinterpret.cast] (original) (raw)
7 Expressions [expr]
7.6 Compound expressions [expr.compound]
7.6.1 Postfix expressions [expr.post]
7.6.1.10 Reinterpret cast [expr.reinterpret.cast]
The result of the expression reinterpret_cast<T>(v) is the result of converting the expression v to type T.
If T is an lvalue reference type or an rvalue reference to function type, the result is an lvalue; if T is an rvalue reference to object type, the result is an xvalue; otherwise, the result is a prvalue and thelvalue-to-rvalue, array-to-pointer, and function-to-pointer standard conversions are performed on the expression v.
Conversions that can be performed explicitly using reinterpret_cast are listed below.
No other conversion can be performed explicitly using reinterpret_cast.
The reinterpret_cast operator shall not cast away constness ([expr.const.cast]).
An expression of integral, enumeration, pointer, or pointer-to-member type can be explicitly converted to its own type; such a cast yields the value of its operand.
[Note 1:
The mapping performed by reinterpret_cast might, or might not, produce a representation different from the original value.
— _end note_]
A pointer can be explicitly converted to any integral type large enough to hold all values of its type.
The mapping function is implementation-defined.
[Note 2:
It is intended to be unsurprising to those who know the addressing structure of the underlying machine.
— _end note_]
A value of type std::nullptr_t can be converted to an integral type; the conversion has the same meaning and validity as a conversion of(void*)0 to the integral type.
[Note 3:
A reinterpret_castcannot be used to convert a value of any type to the typestd::nullptr_t.
— _end note_]
A value of integral type or enumeration type can be explicitly converted to a pointer.
A pointer converted to an integer of sufficient size (if any such exists on the implementation) and back to the same pointer type will have its original value ([basic.compound]);mappings between pointers and integers are otherwiseimplementation-defined.
A function pointer can be explicitly converted to a function pointer of a different type.
[Note 4:
The effect of calling a function through a pointer to a function type ([dcl.fct]) that is not the same as the type used in the definition of the function is undefined ([expr.call]).
— _end note_]
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, the result of such a pointer conversion is unspecified.
An object pointer can be explicitly converted to an object pointer of a different type.53
When a prvalue v of object pointer type is converted to the object pointer type “pointer to cv T”, the result is static_cast<cv T*>(static_cast<cv void*>(v)).
[Note 5:
Converting a pointer of type “pointer to T1” that points to an object of type T1to the type “pointer to T2” (where T2 is an object type and the alignment requirements of T2are no stricter than those of T1) and back to its original type yields the original pointer value.
— _end note_]
Converting a function pointer to an object pointer type or vice versa is conditionally-supported.
The meaning of such a conversion isimplementation-defined, except that if an implementation supports conversions in both directions, converting a prvalue of one type to the other type and back, possibly with different cv-qualification, shall yield the original pointer value.
The null pointer value ([basic.compound]) is converted to the null pointer value of the destination type.
[Note 6:
A null pointer constant of type std::nullptr_t cannot be converted to a pointer type, and a null pointer constant of integral type is not necessarily converted to a null pointer value.
— _end note_]
A prvalue of type “pointer to member of X of type T1” can be explicitly converted to a prvalue of a different type “pointer to member ofY of type T2” if T1 and T2 are both function types or both object types.54
The null member pointer value ([conv.mem]) is converted to the null member pointer value of the destination type.
The result 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-function type and back to its original type yields the original pointer-to-member value.
- Converting a prvalue of type “pointer to data member of Xof type T1” to the type “pointer to data member of Yof 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.
If v is a glvalue of type T1, designating an object or function x, it can be cast to the type “reference to T2” if an expression of type “pointer to T1” can be explicitly converted to the type “pointer to T2” using a reinterpret_cast.
The result is that of *reinterpret_cast<T2 *>(p)where p is a pointer to _x_of type “pointer to T1”.