Yes, C++ has specific rules. The standard explicitly allows type punning using memcpy in certain situations (not the case in the original example), known as the “strict aliasing rule.” Worth reading about
The purpose of strict aliasing and related rules is to enable type-based alias analysis, which would be decimated if a program can validly create a situation where two pointers to unrelated types (e.g., an int* and a float*) could simultaneously exist and both can be used to load or store the same memory (see this email on SG12 reflector). Thus, any technique that is seemingly capable of creating such a situation necessarily invokes undefined behavior.
When it is needed to interpret the bytes of an object as a value of a different type, std::memcpy or std::bit_cast(since C++20) can be used: