Result of some reinterpret_cast conversions
suggest changeThe result of a reinterpret_cast from one function pointer type to another, or one function reference type to another, is unspecified, except that converting that result back to the original type yields the original value. Calling through the mismatched type is undefined behavior. Example:
int f();
auto fp = reinterpret_cast<int(*)(int)>(&f); // fp has unspecified value
A reinterpret_cast between object pointer types, or between object reference types, has been defined since C++11. It is equivalent to static_cast through void*. Example:
int x = 42;
char* p = reinterpret_cast<char*>(&x); // points at the first byte of x
Before C++11 the result was unspecified, though with most compilers it was already equivalent to static_cast<char*>(static_cast<void*>(&x)). Converting back to int* requires the address to be correctly aligned for int. See type punning conversion for more details.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents