3 ms·
In addition to C11's anonymous untagged members, struct foo { union { int i; double d; }; } Visual Studio also supports anonymous tagged members, union b
by wahern 7y ago
In addition to C11's anonymous untagged members,
struct foo { union { int i; double d; }; }
Visual Studio also supports anonymous tagged members,
union bar { int i; double d; };
struct foo { union bar; };
Is the latter more common in Microsoft land? I've always thought the latter were more useful and wished they were standardized. Anonymous tagged fields make it possible to reuse plain struct and union definitions, permitting a simple form of inheritance; whereas with untagged fields you have to rely on macros for sharing definitions.
GCC (https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html) and clang (confirmed macOS clang-1001.0.46.4) support anonymous tagged members with -fms-extensions, but I can't bring myself to make use of it as I still try to keep many of my projects working with Sun Studio and otherwise try to avoid non-standard features.
- asveikau 7y ago> Is the latter more common in Microsoft land? I don't know if I have an authoritative sample on that. I saw it exactly once when I worked at MS and I thought it was weird. But I guess it aids in the "psuedo-inheritance" game that people play with structs and their first member being a "base class". Like GObject or gtk+ do. Or struct sockaddr, which duplicates the first few fields.