Okay, not valid because somewhere it's written that "this is undefined behaviour", but compilable, and it does what I wrote. Well, depending on which compiler you use, with some you get an output of 1, with others you get an output of 5. But that's kinda my point.
It feels to me that just writing somewhere in some documentation "this is undefined behaviour" is also bad imho, which is one of my biggest grips with C++. So many "undefined behaviour"s that were left that way for "performance", instead of properly addressing/closing them. It's like laying out a minefield for a new programmer, and telling them that the map for the location of the mines is in a hut 300m away from the minefield. And when they step on a mine you scold them and tell them the mine was clearly documented on the map in the hut.
View all comments
Show parent comment
2
u/KuntaStillSingle 11d ago
That is not valid: https://en.cppreference.com/cpp/language/reinterpret_cast#Type_accessibility