I have to generate a data structure that contains certain fields only under certain condition. This typically always translates to something like the following
struct MyStruct {
int alwaysHere;
#ifdef WHATEVER
bool mightBeHere;
#endif
char somethingElse;
#if SOME_CONSTANT > SOME_VALUE
uint8_t alywasHereButDifferentSize;
#else
uint16_t alywasHereButDifferentSize;
#endif
...
};
From my point of view this gets easily ugly to look at, and unreadable. Without even talking about the code that handle those fields, usually under ifdefs too.
I'm looking for an elegant way to achieve the same result without adding any overhead whatsoever, but with a code much more readable. Template specialization seems a bit excessive, but it seems to me to be the only alternative.
Is C++11 adding anything at all to deal with this situation?
Any suggestion would be appreciated.