Is it possible to un-const typeof in gcc pure C?

后端 未结 5 1239
误落风尘
误落风尘 2021-02-19 14:42

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

5条回答
  •  盖世英雄少女心
    2021-02-19 15:36

    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.

提交回复
热议问题