I was working on my Roguelike engine and something I've made use of is anonymous structs and unions, which come as part of C11.
struct tagged {
unsigned int top_type;
struct {
int b;
};
struct {
unsigned int key_type;
union {
char c;
char text[32];
unsigned int keycode;
};
};
};
All struct and union members accessible on the same flattened level. Now you might wonder what happens if you want to share a part of this in two locations. You could make a #define and hack it in place anonymously, or you could take advantage of a custom microsoft extension to C (also available in GCC).
union shared_key_value_u {
char c;
char text[32];
unsigned int keycode;
};
struct shared_key_entry_s {
unsigned int key_type;
union shared_key_value_u;
};
struct location1_s {
unsigned int top_type;
struct {
int b;
};
struct shared_key_entry_s;
};
union location2_u {
struct shared_key_entry_s;
unsigned int v1;
struct {
int aa;
char cc;
};
};
This allows me to write much nicer code. Honestly, I think it should have been part of the C11 standard. It's portable anyway, as you can pass "-fms-extensions" to GCC.
The only problem is that Intellisense doesn't implement the same grammar as the VS compiler, so it chokes and claims an error on every single member reference that compiles without error. Which, floods your editor view with red incorrect noise.
Such a pity. The weird thing is that Intellisense will work, if you limit your usage of this feature, perhaps to 'struct location1_s' or 'struct shared_key_entry_s'. But if you throw in the 'union location2_u' case, then all the other cases now choke in Intellisense where they didn't before. So annoying.
I hope they fix this in VS2015 which is coming out in a month.