News for hackers. Stuff that matters.
Re:C++ Patterns: The Badge by GrayShade on Wednesday June 12, 2019 @04:22AM (#20162718)
Isn't casting unrelated types undefined behavior?
Re:C++ Patterns: The Badge by brianush1 on Wednesday June 12, 2019 @08:26AM (#20163809)
Yes, in the standard it isn't defined, but on most--or maybe even all?--systems and compilers it's perfectly defined, since there is no reason why a compiler would layout two different empty structs in different ways; they're both empty anyways.

Edit: Actually, from reading some of the other comments in the thread, the standard apparently specifies that empty structs are 1 byte in size, so technically they would both be the same size, so casting between them should be fine.

Re:C++ Patterns: The Badge by kazinator on Wednesday June 12, 2019 @12:38PM (#20166504)
It's not defined on systems unless a document spells it out. However, the actual behavior can be deduced to be harmless from black-box testing. compiler source code and generate object code.
Re:C++ Patterns: The Badge by kazinator on Wednesday June 12, 2019 @11:51AM (#20165918)
Problem is that it doesn't require a diagnostic, and the actual behavior of casting one empty struct foo {} to another empty struct bar {} is likely harmless and portable.