You can use reinterpret_cast if you really need to. You donβt even have to play with pointers / addresses, as other answers mention. for instance
int i; reinterpret_cast<float&>(i) = 10; std::cout << std::endl << i << " " << reinterpret_cast<float&>(i) << std::endl;
also works (and prints 1092616192 10 if you are qurious;).
EDIT:
From the C ++ standard (about reinterpret_cast):
5.2.10.7 A pointer to an object can be explicitly converted to a pointer to an object of another type. Except that the conversion of the rvalue type from "pointer to T1" to the type "pointer to T2" (where T1 and T2 are object types and where the alignment requirements of T2 are no more stringent than that of T1) and back to its original type is original the value of the pointer, the result of such a pointer conversion is not defined.
5.2.10.10 10 The lvalue expression of type T1 can be passed to the type "reference to T2" if the expression of the type "pointer to T1" can be explicitly converted to the type "pointer to T2" using reinterpret_cast. That is, the reinterpret_cast<T&>(x) link has the same effect as the *reinterpret_cast<T*>(&x) conversion with built-in and * operators. The result is an lvalue that references the same object as the lvalue source, but with a different type. Temporary creation is not created, the copy is missing (12.1) or the conversion functions (12.3) are not called.67)
Thus, it seems that consecutive reinterpretation pointers are not undefined behavior, and using links has the same result as the resulting address, redial, and deferral pointer. I still claim that this is not undefined behavior.
source share