cppreference/reinterpret_cast conversion/Explanation says
Unlike
static_cast
, but likeconst_cast
, the reinterpret_cast 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 expression as if it had the type new-type.
Only the following conversions can be done with reinterpret_cast, except when such conversions would cast away constness or volatility.
...
5) Any object pointer type T1* can be converted to another object pointer type cv T2*. This is exactly equivalent tostatic_cast<cv T2*>(static_cast<cv void*>(expression))
(which implies that if T2's alignment requirement is not stricter than T1'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)
I thought that the reinterpret_cast guaranteed the same bit pattern, and is always compile-time statement . But in the above quote, there is exception on obscure architectures where pointer representation depends on its type, and the conversion of any object pointer type T1
to another object pointer type T2
is exactly equivalent to static_cast<cv T2>(static_cast<cv void*>(expr) )
. for example,
int a = 10;
void* b = static_cast<void*>(&a); // OK.
static_cast<int*>(b) = 20; // OK. back to the original type.
void* b2 = reinterpret_cast<void*>(&a); // char* b2 = static_cast<void*>(static_cast<void*>(&a) );
static_cast<int*>(b2) = 30; // also OK? because the resulting pointer is equivalent to b, so can be back to the original type.
Is If so, is there difference between b
resolved in run-time(can the bit pattern be changed)?.reinterpret_cast
and static_cast
when do any pointer-to-pointer conversion?.