Creating list of stringized macro arguments with variadics and late expansions

Viewed 240

I have the following problem - given variable number of macro arguments argX to create a list of stringized arguments #argX

Example:

LIST(A, B) -> "A", "B"
LIST(A, B, C) -> "A", "B", "C"

I'm using Boost, so the above macro is not too difficult to implement using helper macros for each number of arguments and dispatching LIST(...) to the appropriate LIST_n(arg1, ... argn).

The problem starts when the inputs to LIST are themselves macros. In that case (if I use ... and __VA_ARGS__), the macros get expanded before they are stringized, giving:

#define A 10
LIST(A, B) -> "10", "B"

I want this to work with macros defined in Windows headers and most of the values there are macros (MB_OK, AF_INET, ...), so all I get is a list of stringized numbers.

When not using __VA_ARGS__ everything works fine:

#define A 10
#define LIST_1(arg0) #arg0
LIST_1(A) -> "A"

I've tried several macros that defer the expansion of __VA_ARGS__ to a later time (till LIST_1, for example, that has no variadics), but nothing worked.

Is this even possible to implement using C preprocessor?

1 Answers
Related