8.9 KiB
[expr.reinterpret.cast]
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(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_cast cannot 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 T1 to the type âpointer to T2â (where T2 is an object type and the alignment requirements of T2 are 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 X of type T1â to the type âpointer to data member 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.
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â.
[Note 7:
No temporary is materialized ([conv.rval]) or created, no copy is made, and no constructors ([class.ctor]) or conversion functions ([class.conv]) are called.55
â end note]
The types can have different cv-qualifiers, subject to the overall restriction that a reinterpret_cast cannot cast away constness.
T1 and T2 can have different cv-qualifiers, subject to the overall restriction that a reinterpret_cast cannot cast away constness.
This is sometimes referred to as a type pun when the result refers to the same object as the source glvalue.