Re:Why introduce `std::launder` rather than have the compile...
by gpderetta on Tuesday January 25, 2022 @04:11AM
(#30069835)
But, assuming header is a standard layout type, the following:
header h;
read((char*)&h, sizeof(h));
get_destination(h).write((const char*)&h, sizeof(h));
Is well defined even under strict aliasing.(agree about the two views being live at the same time and memcpy being suboptimal here).
Not to mention, in an example like processing packets, the two views of the data WILL be simultaneously live: you're taking raw bytes on one side, interpreting some part of them as a struct to decide what's happening next, then sending the raw bytes somewhere else.
EDIT: Basically the code would look something like this:
Will this copy the header or not? How about this? Also note that in C we have a somewhat cleaner solution: This would of course be UB in C++ (reading from a union member that is not the last one written to).