0

I'm trying to understand how to handle varargs in a macro function conformingly (i.e. avoiding gcc's comma swallowing ##__VA_ARGS__). I wrote this:

#define IS_DEFINED(ARG) strlen(#ARG)
#define FOO(a, ...) \
    if(IS_DEFINED(__VA_ARGS__)){\
        printf(#a, __VA_ARGS__)\
    } else {\
        printf(#a)\
    }

But it does not generate a valid program test. FOO(1) expands to

if(strlen("")){ printf("1", ) } else { printf("1") };

which does not compile.

Is there a way to handle a case of vararg macro function with one parameter and vararg?

Some Name
  • 8,555
  • 5
  • 27
  • 77

1 Answers1

1

From C23 onwards, you should be able to use __VA_OPT__() for this:


#define FOO(a, ...) \
        printf(#a __VA_OPT__(,) __VA_ARGS__)
Toby Speight
  • 27,591
  • 48
  • 66
  • 103