Unfortunately there are also still a lot of platforms that have yet to get C++20 support and memcpy is not a viable alternative. Hopefully this is less of an issue in the future as major MCU orgs like TI have started switching to LLVM/Clang based compilers which can follow upstream with little to no changes needed.
Edit: I just did some digging and bit_cast does not work for the aforementioned use case since it can copy the data to a new location in memory. This prevents us from being able to reliably use it to write to memory registers.
reinterpret_cast guarantees that it is in-place, so it's the right tool for 0-copy.
What when your compiler starts to take advantage of latitude extended by the Standard, leading your program to produce sporadic, non-reproducible wrong results?
It is best not to code any UB. But it is often hard to be certain, around reinterpret_cast.
You rarely want to be spending time fiddling with the optimizer. Your code shouldn't rely on the optimizer to be efficient: don't use constructs whose semantics mean you are making a copy in places where you don't want a copy.
reintrepret_cast is hard to use correctly, but you only have to get it right once, and it has the right semantics, so no compiler changes will suddenly make it wrong or slow. memcpy and bit_cast have the wrong semantics, so they may change for unexpected reasons. In fact, it would be perfectly reasonable for a future version of the compiler to never optimize away the copy implied by memcpy and bit_cast.
So yes, you can confidently rely on gcc or clang to merge local variables, even at -O1. If they suddenly don't do that after an update, it's a serious regression, and their mailing lists should be more than happy to receive that bug report.
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:
char* buf = new char[100];
read(buf, sizeof(header));
header h;
memcpy(&h, buf, sizeof(header))
get_destination(h).write(buf);
Will this copy the header or not? How about this? char* buf = new char[100];
read(buf, sizeof(header));
header *h = reinterpret_cast<header*>(buf); //assume we are compiling with "-fno-strict-alias", and that we know there are no alignment/packing concerns
get_destination(h).write(buf);
Also note that in C we have a somewhat cleaner solution: union buf_header {
header h;
char buf[100];
} buffer;
read(buffer.buf, sizeof(header));
get_destination(buffer.h).write(buffer.buf);
This would of course be UB in C++ (reading from a union member that is not the last one written to). 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).
For small objects bitcast works just fine because the result end up in registers were you would have loaded the original objects anyway.
But in fact memcpy to a local is very often a better alternative, pre-C++20. Compilers optimize it away.