I want to initialize different union member depending if a macro expression is a constant or a variable.
So far I've found GCC extension __builtin_constant_p()
which evaluates an expression and returns 1 if it is a constant and 0 otherwise. Which is exactly what I need. However I have yet to found a way how to initialize different union members based on this information.
Below is the code example:
#define TOKEN 10
typedef union
{
void* pv_variable;
int literal;
} foo_t;
foo_t test_array[] =
{
{.literal = 500},
{.pv_variable = NULL}
// Below doesnt work. How to make it work??
//
// {(TOKEN == 10) ? (.literal) : (.pv_variable) = 10},
// {__builtin_choose_expr(__builtin_constant_p(TOKEN), .literal = 10, .pv_variable = NULL)},
// {. ## __builtin_choose_expr(__builtin_constant_p(TOKEN), literal, pv_variable) = 10},
};
EDIT: Clarification on what I want to accomplish is detailed below.
#define INIT_UNION(val) /* TODO: */
#define SOME_VARIABLE some_variable
#define SOME_CONSTANT 10
int some_variable = 20;
typedef union
{
void* pv_variable;
int literal;
} foo_t;
foo_t test_array[] =
{
INIT_UNION(SOME_CONSTANT), /* should produce: {.literal = 10} */
INIT_UNION(SOME_VARIABLE) /* should produce: {.pv_variable = &some_variable} */
};