Re:Int a = 5; a = a++ + ++a; a =? (2011)
by IshKebab on Thursday May 14, 2026 @04:49PM
(#48141041)
They don't really. In fact there are many things that are technically UB but are so common that compilers can't really treat them as UB. E.g. type punning via unions.
[ Parent
| on HN ]
Re:Int a = 5; a = a++ + ++a; a =? (2011)
by IcyWindows on Thursday May 14, 2026 @07:33PM
(#48142609)
Yeah, undefined behavior just means not defined in the specification.
I would argue that most languages only have one compiler so it doesn't matter what is in the specification.
Re:Int a = 5; a = a++ + ++a; a =? (2011)
by el_pollo_diablo on Thursday May 14, 2026 @05:20PM
(#48141406)
Type punning via unions is not UB in C in general, but it is in C++ IIRC.
I write "in general" because, as with other forms of memory reinterpretation (memcpy or copy through a character type), evaluating a trap representation triggers UB.
The short version is that it's fine in C++ as long as you only read the member that was last written to or a char type.
Re:Int a = 5; a = a++ + ++a; a =? (2011)
by 1718627440 on Friday May 15, 2026 @08:51AM
(#48147960)
And a slightly longer version is, that there are three types involved: the type of access, the effective type of the object[0], and the type of the variable. The type of the variable is only for the compiler to emit warnings, as long as the effective type and the type of access are equal, it isn't UB.
[0] the C meaning of an object, not the C++ one