Remix.run Logo
jandrewrogers 3 hours ago

There was already a legal way to achieve this that everyone should already have been using (laundering a pointer through a no-op memmove). Using reinterpret_cast here is a bug.

The "start_lifetime_as" facility does one additional thing beyond providing a tidy standard name for the memory laundering incantation. Semantically it doesn't touch the memory whereas the no-op memmove intrinsically does. In practice, this makes little difference, since the compiler could see that the memmove was a no-op and optimized accordingly.

kevin_thibedeau 3 hours ago | parent | next [-]

This still has unresolved alignment issues that blow up outside the amd64 ecosystem.

jandrewrogers 2 hours ago | parent [-]

Is this just a basic lack of alignment enforcement or is there a bigger issue?

szmarczak 2 hours ago | parent | prev [-]

No because the object does not exist after std::launder. It only exists after std::start_lifetime_as. The bytes being there says nothing about the object, per the C++ standard.

jandrewrogers 2 hours ago | parent [-]

The compiler will create an implicit lifetime type at the memmove destination as required to give it defined behavior. Technically you don't even need std::launder, it is just far more convenient than the alternative.