Variadic UNUSED function/macro

后端 未结 4 1121
陌清茗
陌清茗 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:15

    I took Dabo's (https://stackoverflow.com/a/23238813/5126486) awesome solution and improved a little bit so it's easier to extend to more than 5:

    #define UNUSED0()
    #define UNUSED1(a)                  (void)(a)
    #define UNUSED2(a,b)                (void)(a),UNUSED1(b)
    #define UNUSED3(a,b,c)              (void)(a),UNUSED2(b,c)
    #define UNUSED4(a,b,c,d)            (void)(a),UNUSED3(b,c,d)
    #define UNUSED5(a,b,c,d,e)          (void)(a),UNUSED4(b,c,d,e)
    
    #define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5, N,...) N
    #define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL(100, ##__VA_ARGS__, 5, 4, 3, 2, 1, 0 )
    
    #define ALL_UNUSED_IMPL_(nargs) UNUSED ## nargs
    #define ALL_UNUSED_IMPL(nargs) ALL_UNUSED_IMPL_(nargs)
    #define ALL_UNUSED(...) ALL_UNUSED_IMPL( VA_NUM_ARGS(__VA_ARGS__))(__VA_ARGS__ )
    

提交回复
热议问题