VC Bug? Compiler Unable to Count __VA_ARGS__' Arguments

670 views Asked by At

Microsoft Visual Studio Professional 2015 Version 14.0.25431.01 Update 3 issues errors when compiling the code below. Looks like a bug to me.


Thank you.

#include <iostream>

#define A( a, b, c, ... ) #__VA_ARGS__
#define B( ... ) A(__VA_ARGS__)

int main()
{
  // warning C4003: not enough actual parameters for macro 'A'
  // error C2059: syntax error: ';'
  std::cout << B( 1, 2, 3, 4 ); // should print '4'                                    
  return 0;
}
1

There are 1 answers

5
AudioBubble On BEST ANSWER

It looks like a bug to me too. It's possible to work around it with another layer of macros:

#define EXPAND(...) __VA_ARGS__
#define A( a, b, c, ... ) #__VA_ARGS__
#define B( ... ) EXPAND(EXPAND(A) (__VA_ARGS__))

The idea is that first, EXPAND(A) gets expanded to A and (__VA_ARGS__) gets expanded to ( 1, 2, 3, 4 ). Then, you're left with A ( 1, 2, 3, 4 ), which VC++ understands if you force it to expand yet again.