I am trying to apply the X Macro concept, in order to have the possibility to initialize all struct members to a custom default (invalid) value. I write the following code:<
Let's pretend that we are the preprocessor and encountering the line:
static struct foo test = foo_DEFAULT_VALUE;
Pass 1:
static struct foo test = { LIST_OF_STRUCT_MEMBERS_foo };
Pass 2:
static struct foo test = { X(a) X(b) X(c) };
Pass 3: Nothing to expand as X is undefined on this line.
One workaround could be defining a const variable (possibly but not necessarily static) to be used as default value:
#define X(name) -1,
static const struct foo foo_DEFAULT_VALUE = { LIST_OF_STRUCT_MEMBERS_foo };
#undef X
Which generates:
static const struct foo foo_DEFAULT_VALUE = { -1, -1, -1, };