I have a macro that uses GCC\'s typeof to create a variable of the same type of a macro argument. The problem is: if that argument has const
type, the variable crea
You could use a C11 _Generic
selection to map from const
to non-const
type:
#define DECR_(t, x) ({ t y = (x); --y; y; })
#define DECR(x) _Generic((x), \
int: DECR_(int, (x)), \
const int: DECR_(int, (x)), \
long: DECR_(long, (x)), \
const long: DECR_(long, (x)), \
unsigned int: DECR_(unsigned int, (x)), \
const unsigned int: DECR_(unsigned int, (x)), \
long long: DECR_(long long, (x)), \
const long long: DECR_(long long, (x)))
Although it involves a LOT of typing, even if you only need to cover integral types. C11 is also far from being widely available these days. Live example at Coliru.