4 ms·
I would love for `union`s to be federated, that is, a type could declare itself as thought it was part of a union with another type, without having to pre-decla
by EPWN3D 1y ago
I would love for `union`s to be federated, that is, a type could declare itself as thought it was part of a union with another type, without having to pre-declare all possible types in one place.
- o11c 1y agoFor layout-compatible types, you can often just include a `_base` member in each child. Maybe twice (once named and once unnamed) to avoid excess typing - I don't understand the common-initial-subsequence rule but people do this enough that compilers have to allow it.
- manwe150 1y agoI don't think compilers typically allow that: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14319 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14319
- Joker_vD 1y agoCan't you just declare anonymous unions whenever? E.g. struct foo { ... }; struct bar { ... }; struct bar check_this_out(struct foo foo) { return ((union { struct foo foo; struct bar bar; }){ .foo = foo}).bar; } struct bar *also_this(struct foo *foo) { union { struct foo foo; struct bar bar; } *tmp = (void*)foo; return &tmp->bar; }