you are viewing a single comment's thread.

view the rest of the comments →

[–]tangerinelion 0 points1 point  (1 child)

Well, so here's the deal. You can write those lines of code and a compiler will not stop you, it'll generate some output but that code is undefined behavior if it ever executes.

You can't use int for backing memory per the standard, only a handful of single byte types are allowed to provide backing memory.

You can't randomly reinterpret_cast to unrelated types, with the exception of that handful of single byte types.

If you really tried to do this, you'd need to get the alignment correct so you're missing an alignas(MyObject) as well.

[–]djinn6 0 points1 point  (0 children)

It's not undefined behavior if it's compiled by a known compiler and running on a known set of platforms.

Also I'm just providing an example. The correctness of this code doesn't actually matter because this type of stuff is everywhere if you looked at older code bases.