What does __VA_ARGS__ in a macro mean?

前端 未结 2 525
忘掉有多难
忘掉有多难 2021-02-01 06:16
/* Debugging */
#ifdef DEBUG_THRU_UART0
#   define DEBUG(...)  printString (__VA_ARGS__)
#else
void dummyFunc(void);
#   define DEBUG(...)  dummyFunc()   
#endif
         


        
2条回答
  •  孤城傲影
    2021-02-01 07:05

    The dots are called, together with the __VA_ARGS__, variadic macros

    When the macro is invoked, all the tokens in its argument list [...], including any commas, become the variable argument. This sequence of tokens replaces the identifier VA_ARGS in the macro body wherever it appears.

    source, bold emphasis of mine.

    A sample of usage:

    #ifdef DEBUG_THRU_UART0
    #   define DEBUG(...)  printString (__VA_ARGS__)
    #else
    void dummyFunc(void);
    #   define DEBUG(...)  dummyFunc()   
    #endif
    DEBUG(1,2,3); //calls printString(1,2,3) or dummyFunc() depending on
                  //-DDEBUG_THRU_UART0 compiler define was given or not, when compiling.
    

提交回复
热议问题