3 ms·
Not true. You actually can. Aliasing is about viewing the same bits as two different types (what `reinterpret_cast`, union-based type punning, and cast pointers
by colejohnson66 6y ago
Not true. You actually can. Aliasing is about viewing the same bits as two different types (what `reinterpret_cast`, union-based type punning, and cast pointers do) `memcpy` copies the bits to a new location which doesn’t violate aliasing. I don’t have time to find the reference in the standard, but cppreference.com[0] says it’s ok:
> Where strict aliasing prohibits examining the same memory as values of two different types, std::memcpy may be used to convert the values.
[0]: https://en.cppreference.com/w/cpp/string/byte/memcpy https://en.cppreference.com/w/cpp/string/byte/memcpy
- kazinator 6y agoWho is cppreference.com? The domain registration is concealed, just like that of the other dubious site, cplusplus.com. I don't see any such a claim about memcpy having magic powers in the N4659 C++ draft. All it says is that the underlying bytes of an object can be copied to an array of char, unsigned char, or std::byte. Then if that array is copied back into the object, the object subsequently holds its original value. Anything more about memcpy comes from C by reference, and that would be about the last document on Earth to grant definedness and portability to something like this.