C Preprocessor Remove Trailing Comma

前端 未结 2 604
一向
一向 2021-02-19 07:30

I have a macro like this:

#define C( a... ) ( char *[] ){ a, 0 }

This works for non-empty arguments:

C( \"a\", \"b\" ) => (          


        
2条回答
  •  遇见更好的自我
    2021-02-19 08:00

    Check out GCC's __VA_OPT__() function macro that can be used within a variadic macro.

    #define C(...) (char *[]) { __VA_ARGS__ __VA_OPT__(,) 0 }
    
    C("a", "b");   expands to (char *[]) { "a", "b" , 0 };
    C();           expands to (char *[]) { 0 };
    

    The arguments passed to __VA_OPT__() will only be expanded if __VA_ARGS__ is non-empty.

提交回复
热议问题