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
If you don't mind the possible arithmetic promotion you can do this:
#define DECR(x) ({typeof(x + 0) y; y = x; y--; y;})
The trick is that the expression for typeof
is x + 0
, which is a r-value, and so the l-value-constness (which is what you want to avoid) is lost.
The same trick can be done with 1 * x
, but curiously enough, +x
and -x
don't work.