Msvc++ Variadic Macro Expansion

MSVC++ variadic macro expansion

Coincidentally, I happened to run into this problem just today, and after enough effort I think I've found a solution for my own purposes. The bug is MSVC treats __VA_ARGS__ as a single token in argument lists. But you can work around this by not using it directly within a macro call argument list. This comment suggests the start of an answer to your problems:

#define VA_NARGS(...) VA_NUM_ARGS_IMPL_((__VA_ARGS__, 5,4,3,2,1))
#define VA_NARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NARGS_IMPL(_1,_2,_3,_4,_5,N,...) N

But then I suspect you'll likely run into the issue of making sure that gets fully expanded to the actual "N" you want, and not to VA_NARGS_IMPL (arg1, arg2, 5, 4, 3, 2, 1), say. I found that my code (which looked like yours) had to change to expand MAC##code all as one unit, and then that had to be separately combined with the argument list. Here's the code that I found worked for me:

#define ASSERT_HELPER1(expr) singleArgumentExpansion(expr)
#define ASSERT_HELPER2(expr, explain) \
twoArgumentExpansion(expr, explain)

/*
* Count the number of arguments passed to ASSERT, very carefully
* tiptoeing around an MSVC bug where it improperly expands __VA_ARGS__ as a
* single token in argument lists. See these URLs for details:
*
* http://connect.microsoft.com/VisualStudio/feedback/details/380090/variadic-macro-replacement
* http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/#comment-644
*/
#define COUNT_ASSERT_ARGS_IMPL2(_1, _2, count, ...) \
count
#define COUNT_ASSERT_ARGS_IMPL(args) \
COUNT_ASSERT_ARGS_IMPL2 args
#define COUNT_ASSERT_ARGS(...) \
COUNT_ASSERT_ARGS_IMPL((__VA_ARGS__, 2, 1, 0))
/* Pick the right helper macro to invoke. */
#define ASSERT_CHOOSE_HELPER2(count) ASSERT_HELPER##count
#define ASSERT_CHOOSE_HELPER1(count) ASSERT_CHOOSE_HELPER2(count)
#define ASSERT_CHOOSE_HELPER(count) ASSERT_CHOOSE_HELPER1(count)
/* The actual macro. */
#define ASSERT_GLUE(x, y) x y
#define ASSERT(...) \
ASSERT_GLUE(ASSERT_CHOOSE_HELPER(COUNT_ASSERT_ARGS(__VA_ARGS__)), \
(__VA_ARGS__))

int foo()
{
ASSERT(one); // singleArgumentExpansion(one)
ASSERT(two, "foopy"); // twoArgumentExpansion(two, "foopy")
}

My mind is too much mush after a few hours solving my own issues to then go and completely solve yours, I'm sorry to say. :-) But I think this is enough to get you to something that works, with a little work.

__VA_ARGS__ expansion using MSVC

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.

How to fix variadic macro related issues with macro overloading in MSVC++ (Microsoft Visual studio)?

I'm not very experienced with the MSVC bug other than knowing it exists, but I was able to apply the workaround in the other answer as follows:

#define MSVC_BUG(MACRO, ARGS) MACRO ARGS  // name to remind that bug fix is due to MSVC :-)

#define NUM_ARGS_2(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, TOTAL, ...) TOTAL
#define NUM_ARGS_1(...) MSVC_BUG(NUM_ARGS_2, (__VA_ARGS__))
#define NUM_ARGS(...) NUM_ARGS_1(__VA_ARGS__, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
#define VA_MACRO(MACRO, ...) MSVC_BUG(CONCATE, (MACRO, NUM_ARGS(__VA_ARGS__)))(__VA_ARGS__)

This also produces appropriate preprocessor output on the latest GCC and Clang. It would not surprise me to learn that there's a way to work around this without calling MSVC_BUG macro for "indirect expansion" twice, but I didn't find it.

Variadic macro expansion's going wrong

I think gcc is correct. Although C11 6.10.3/12 describes the invocation of COMB as having two arguments (ADD and 1,2), once COMB has been expanded the resulting token sequence is ADD ( 1 , 2 ), and 6.10.3.4/1 is clear that the result of the first replacement is rescanned as a preprocessing token sequence. An argument from a previous step that consisted of multiple tokens is not somehow glued into a single token for rescanning.

6.10.3.4/1:

After all parameters in the replacement list have been substituted and # and ##
processing has taken place, all placemarker preprocessing tokens are removed. The
resulting preprocessing token sequence is then rescanned
, along with all subsequent
preprocessing tokens of the source file, for more macro names to replace

MSVC doesn't expand __VA_ARGS__ correctly

Edit:
This issue might be resolved by using
/Zc:preprocessor or /experimental:preprocessor option in recent MSVC.
For the details, please see
here.

MSVC's preprocessor seems to behave quite differently from the standard
specification.

Probably the following workaround will help:

#define EXPAND( x ) x
#define F(x, ...) X = x and VA_ARGS = __VA_ARGS__
#define G(...) EXPAND( F(__VA_ARGS__) )

How to Correctly use Variadic Macros to Call Other Macros

Apparently, MSVC has a bug that does not allow it to directly pass __VA_ARGS__ successfully to another macro.

The sample that failed for me was:

#define A(X, ...) A_##X(__VA_ARGS__)
#define A_TEST(A, B) ((A)+(B))

printf("%d\n", A(TEST, 1, 2));

However, if I wrap the __VA_ARGS__ within parentheses, the contents are treated as a single argument. I can pass this to another macro to actually do the invocation, and it works.

#define A(X, ...) A_(X, (__VA_ARGS__))
#define A_(X, Y) A_##X Y
#define A_TEST(A, B) ((A)+(B))


Related Topics



Leave a reply



Submit