cpp/language/reinterpret cast

Converts between types by reinterpreting the underlying bit pattern.

Syntax
Returns a value of type.

Explanation
Unlike, but like , the expression does not compile to any CPU instructions (except when converting between integers and pointers or on obscure architectures where pointer representation depends on its type). It is purely a compile-time directive which instructs the compiler to treat as if it had the type.

Only the following conversions can be done with, except when such conversions would cast away constness or volatility.

@1@ An expression of integral, enumeration, pointer, or pointer-to-member type can be converted to its own type. The resulting value is the same as the value of. @2@ A pointer can be converted to any integral type large enough to hold all values of its type (e.g. to std). @3@ A value of any integral or enumeration type can be converted to a pointer type. A pointer converted to an integer of sufficient size and back to the same pointer type is guaranteed to have its original value, otherwise the resulting pointer cannot be dereferenced safely (the round-trip conversion in the opposite direction is not guaranteed; the same pointer may have multiple integer representations) The null pointer constant null or integer zero is not guaranteed to yield the null pointer value of the target type; or  should be used for this purpose.

@5@ Any object pointer type can be converted to another object pointer type. This is exactly equivalent to (which implies that if 's alignment requirement is not stricter than 's, the value of the pointer does not change and conversion of the resulting pointer back to its original type yields the original value). In any case, the resulting pointer may only be dereferenced safely if allowed by the type aliasing rules (see below). @6@ An expression of type  can be converted to reference to another type. The result is that of, where is a pointer of type “pointer to ” to the object designated by. No temporary is created, no copy is made, no constructors or conversion functions are called. The resulting reference can only be accessed safely if allowed by the type aliasing rules (see below). @7@ Any pointer to function can be converted to a pointer to a different function type. Calling the function through a pointer to a different function type is undefined, but converting such pointer back to pointer to the original function type yields the pointer to the original function. @8@ On some implementations (in particular, on any POSIX compatible system as required by ), a function pointer can be converted to or any other object pointer, or vice versa. If the implementation supports conversion in both directions, conversion to the original type yields the original value, otherwise the resulting pointer cannot be dereferenced or called safely. @9@ The null pointer value of any pointer type can be converted to any other pointer type, resulting in the null pointer value of that type. Note that the null pointer constant or any other value of type std cannot be converted to a pointer with : implicit conversion or  should be used for this purpose. @10@ A pointer to member function can be converted to pointer to a different member function of a different type. Conversion back to the original type yields the original value, otherwise the resulting pointer cannot be used safely. @11@ A pointer to member object of some class can be converted to a pointer to another member object of another class. If 's alignment is not stricter than 's, conversion back to the original type yields the original value, otherwise the resulting pointer cannot be used safely.

Type aliasing
Whenever an attempt is made to read or modify the stored value of an object of type through a glvalue of type, the behavior is undefined unless one of the following is true:
 * and are similar.
 * is the (possibly -qualified) signed or unsigned variant of.
 * is, or : this permits examination of the   of any object as an array of bytes.

Informally, two types are similar if, ignoring top-level cv-qualification:
 * they are the same type; or
 * they are both pointers, and the pointed-to types are similar; or
 * they are both pointers to member of the same class, and the types of the pointed-to members are similar; or

For example:
 * and are similar;
 * and are similar;
 * and are similar;
 * and are not similar;
 * and are not similar;
 * and are not similar;
 * and are similar (they are the same type);
 * and are not similar.

This rule enables type-based alias analysis, in which a compiler assumes that the value read through a glvalue of one type is not modified by a write to a glvalue of a different type (subject to the exceptions noted above).

Note that many C++ compilers relax this rule, as a non-standard language extension, to allow wrong-type access through the inactive member of a (such access is not undefined in C).