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?
I'm sorry but there is now way to do this on msvc. Because of classic bugs in their preprocessor(see here and here) they treat
__VA_ARGS__
as a single argument. To break it up into separate arguments requires applying another scan which will then expand the macros as well. On a C99 preprocessor you can inhibit the expansion of__VA_ARGS__
using an empty placeholder:This will work for up to 8 arguments on gcc and clang 3.4 or higher.