2 ms·
Structs aren't really any more fundamental than tagged unions though. They also contain bytes which are hidden from the language (padding). If a struct can have
by nicoburns 6y ago
Structs aren't really any more fundamental than tagged unions though. They also contain bytes which are hidden from the language (padding). If a struct can have automatically generated padding bytes, why shouldn't a tagged union have an automatically generated tag?
- eqvinox 6y agoMostly because C structs have a very exactly bound memory representation, and padding bytes are just about the only thing you can - mostly - get away with not caring about. Coincidentally, even padding bytes break things technically. Since they're undefined, you - in theory - can't ever calculate a bytewise hash value over a struct that has any padding bytes. It's still done frequently, it's just nulled out beforehand and copying is bytewise anyway. Doesn't change the fact that to the spec letter it's undefined behaviour. GCC also added __builtin_clear_padding() recently. Hidden tags ... no. Just no.
- nicoburns 6y agoDoesn't need to be hidden. Just usable with language features (e.g. pattern matching) without accessing it explicitly.