Variadic UNUSED function/macro

后端 未结 4 1118
陌清茗
陌清茗 2021-02-04 11:25

A well-known and portable way to suppress C compiler warnings about unused variables is (see unused parameter warnings in C code):

#define UNUSED(x) (void)(x)
         


        
4条回答
  •  无人及你
    2021-02-04 12:16

    You can use compile time __VA_ARGS__ macro.

    #define UNUSED(...) (void)(__VA_ARGS__)

    UPDATE: After lot of trials, I came up to an optimized solution:

    #define UNUSED(...)  __VA_ARGS__
    
    int main()
    {
        int e, x;
        char **a, **b, *c, d[45];
        x = x, UNUSED(a, b, c, d, e), x; 
    
        return 0;
    }
    

    NOTES:

    1. It doesn't eliminate warnings completely but reduces them to just 3 same type of warnings:
      warning: value computed is not used

    2. The first and last x ensure assignment of same datatypes.

    3. I will say it is optimized because for any number of unused variables it gives 3 warnings (I may be wrong, please test it yourself and do report me if you get more) and the amount of code (MACRO manipulations) required to achieve it is less.

    4. I am still working on it, will post if I reach to any better solution.

提交回复
热议问题