问题
I found a question showing how to overload macros based on the number of arguments : Overloading Macro on Number of Arguments
But as they say, it's not working using MSVC because MSVC expands __VA_ARGS__
into a single token instead of a list of arguments (arg1, arg2, arg3
).
They point to another question where a work around is given : MSVC doesn't expand __VA_ARGS__ correctly But not explained at all, so I can't adapt it to my own case since I can't understand it.
Could you please explain how this workaround works ?
回答1:
The workaround in question is this:
#define EXPAND( x ) x
#define F(x, ...) X = x and VA_ARGS = __VA_ARGS__
#define G(...) EXPAND( F(__VA_ARGS__) )
The idea is that given an existing variadic macro F()
:
#define F(x, ...) X = x and VA_ARGS = __VA_ARGS__
instead of writing your desired variadic wrapper macro as, in this case, ...
#define G(...) F(__VA_ARGS__)
... you write G()
with use of the additional EXPAND()
macro. The actual definition of F()
is not the point, and in particular it doesn't matter for this example that macro expansion does not produce valid C code. Its purpose is to demonstrate the preprocessor's behavior with respect to macro arguments. Specifically, it shows that although MSVC expands __VA_ARGS__
to a single token in a variadic macro, that can be worked around by forcing a double expansion.
For example, using the workaround definition, the preprocessor first expands ...
G(1, 2, 3)
... to ...
EXPAND( F(1, 2, 3) )
... where the 1, 2, 3
is treated as a single token. That tokenization no longer matters when the preprocessor rescans for additional replacements, however: it sees the 1
, 2
, 3
as separate arguments to macro F()
, and expands that as desired to produce the argument to macro EXPAND()
, which just replaces it with itself.
If you think it odd that this works as intended, but the version without EXPAND()
does not work (in MSVC), you are right.
来源:https://stackoverflow.com/questions/32399191/va-args-expansion-using-msvc