Type punning conversion

suggest change

A pointer (resp. reference) to an object type can be converted to a pointer (resp. reference) to any other object type using reinterpret_cast. This does not call any constructors or conversion functions.

int x = 42;
char* p = static_cast<char*>(&x);      // error: static_cast cannot perform this conversion
char* p = reinterpret_cast<char*>(&x); // OK
*p = 'z';                              // maybe this modifies x (see below)

The result of reinterpret_cast represents the same address as the operand, provided that the address is appropriately aligned for the destination type. Otherwise, the result is unspecified.

int x = 42;
char& r = reinterpret_cast<char&>(x);
const void* px = &x;
const void* pr = &r;
assert(px == pr); // should never fire

The result of reinterpret_cast is unspecified, except that a pointer (resp. reference) will survive a round trip from the source type to the destination type and back, as long as the destination type’s alignment requirement is not stricter than that of the source type.

int x = 123;
unsigned int& r1 = reinterpret_cast<unsigned int&>(x);
int& r2 = reinterpret_cast<int&>(r1);
r2 = 456; // sets x to 456

On most implementations, reinterpret_cast does not change the address, but this requirement was not standardized until C++11.

reinterpret_cast can also be used to convert from one pointer-to-data-member type to another, or one pointer-to-member-function type to another.

Use of reinterpret_cast is considered dangerous because reading or writing through a pointer or reference obtained using reinterpret_cast may trigger undefined behaviour when the source and destination types are unrelated.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Explicit type conversions:
* Type punning conversion

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
83 Explicit type conversions
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype