In C++, type punning through pointers and unions is undefined behavior. Even `reinterpret_cast<T>` isn’t allowed because of aliasing (IIRC). The only “defined” way to do type punning is a memcpy. A compiler targeting something like x86 would optimize out the memcpy.
For more information, see the C++20 final draft[0§7.6.1.9]
For more information, see the C++20 final draft[0§7.6.1.9]
[0]: https://isocpp.org/files/papers/N4860.pdf