reinterpret_cast conversion
Converts between types by reinterpreting the underlying bit pattern.
Contents |
[edit] Syntax
reinterpret_cast < new_type > ( expression ) | |||||||||
Returns a value of type new_type.
[edit] Explanation
Unlike static_cast, but like const_cast, the reinterpret_cast expression does not compile to any CPU instructions. It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the type new_type.
Only the following conversions can be done with reintepret_cast, except when such conversions would cast away constness or volatility.
As with all cast expressions, the result is:
- an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;
- an xvalue if new_type is an rvalue reference to object type;
- a prvalue otherwise.
[edit] Keywords
[edit] Type aliasing
When a pointer or reference to object of type T1 is reintrepret_cast (or C-style cast) to a pointer or reference to object of a different type T2, the cast always succeeds, but the resulting pointer or reference may only be accessed if one of the following is true:
- T2 is the (possibly cv-qualified) dynamic type of the object
- T2 and T1 are both (possibly multi-level, possibly cv-qualified at each level) pointers to the same type T3 (since C++11)
- T2 is the (possibly cv-qualified) signed or unsigned variant of the dynamic type of the object
- T2 is an aggregate type or a union type which holds one of the aforementioned types as an element or non-static member (including, recursively, elements of subaggregates and non-static data members of the contained unions)
- T2 is a (possibly cv-qualified) base class of the dynamic type of the object
- T2 is char or unsigned char
If T2 does not satisfy these requirements, accessing the object through the new pointer or reference invokes undefined behavior. This is known as the strict aliasing rule and applies to both C++ and C programming languages.
[edit] Example
Demonstrates some uses of reinterpret_cast:
#include <cstdint> #include <cassert> #include <iostream> int f() { return 42; } int main() { int i = 7; // pointer to integer and back uintptr_t v1 = reinterpret_cast<uintptr_t>(&i); // static_cast is an error std::cout << "The value of &i is 0x" << std::hex << v1 << '\n'; int* p1 = reinterpret_cast<int*>(v1); assert(p1 == &i); // pointer to function to another and back void(*fp1)() = reinterpret_cast<void(*)()>(f); // fp1(); undefined behavior int(*fp2)() = reinterpret_cast<int(*)()>(fp1); std::cout << std::dec << fp2() << '\n'; // safe // type aliasing through pointer char* p2 = reinterpret_cast<char*>(&i); if(p2[0] == '\x7') std::cout << "This system is little-endian\n"; else std::cout << "This system is big-endian\n"; // type aliasing through reference reinterpret_cast<unsigned int&>(i) = 42; std::cout << i << '\n'; }
Output:
The value of &i is 0x7fff352c3580 42 This system is little-endian 42
[edit] See also
const_cast conversion | adds or removes const |
static_cast conversion | performs basic conversions |
dynamic_cast conversion | performs checked polymorphic conversions |