The rules of strict aliasing are mystifying (to me). Is the following code legal or UB?
Assume the buffer comes from a socket, and an Alias type wasn't placement-new'ed anywhere within my code.
struct Alias
{
int64_t a;
int64_t b;
}
int read(const QByteArray& buffer)
{
Alias* alias = reinterpret_cast<Alias*>(buffer.data());
int isThisUB = alias.a;
return isThisUB;
}
I'm not sure if this is UB or not because buffer.data() returns a char*? If it is, does launder'ing it make it well defined?
int read(const QByteArray& buffer)
{
Alias* alias = std::launder(reinterpret_cast<Alias*>(buffer.data()));
int isThisUB = alias.a;
return isThisUB;
}
Do I actually need to use memcpy or some union here?
Reasons I'm confused: