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

后端 未结 5 1249
误落风尘
误落风尘 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:22

    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.

提交回复
热议问题