Search code examples
cmacrosc-preprocessorvariadicvariadic-macros

How to expand macro and delete comma


For example I want to write my own printf() alternative, but I have to perform calculations on the variable arguments:

#define log(fmt_string, ...) my_log(fmt_string, pack_args(__VA_ARGS__), __VA_ARGS__)

where pack_args(...) - is a macro too. How should I change this code to handle the only fmt_string presence scenario?

log("Some message here");

Solution

  • In P99 I have two macros

    #define P00_ARG(                                               \
     _1, _2, _3, _4, _5, _6, _7, _8,                               \
     _9, _10, _11, _12, _13, _14, _15, _16,                        \
      ... etc ...                                                  \
     _153, _154, _155, _156, _157, _158, _159,                     \
     ...) _159
    #define P99_HAS_COMMA(...) P00_ARG(__VA_ARGS__,                \
     1, 1, 1, 1, 1, 1, 1,                                          \
     1, 1, 1, 1, 1, 1, 1, 1,                                       \
      ... etc ....                                                 \
     1, 1, 1, 1, 1, 1, 0, 0)
    

    You can use this to determine if your argument has a comma (so there are more arguments than your format) or not (only a format). You can then use that to construct a call to one of two macros:

    #define log(...) log2(P99_HAS_COMMA(__VA_ARGS__), __VA_ARGS__)
    #define log2(N, ...) log3(N, __VA_ARGS__)
    #define log3(N, ...) log ## N(__VA_ARGS__)
    
    #define log0(FMT)              /* your version with format only goes here */
    #define log1(FMT, __VA_ARGS__) /* your version with more goes here */