gcc does support argument counting macros with zero arguments with the ## __VA_ARGS__
convention. The following works compiled with gcc:
#include <stdio.h>
#define NARGS(...) __NARGS(0, ## __VA_ARGS__, 5,4,3,2,1,0)
#define __NARGS(_0,_1,_2,_3,_4,_5,N,...) N
int main()
{
printf("%d\n", NARGS()); // prints 0
printf("%d\n", NARGS(1)); // prints 1
printf("%d\n", NARGS(1, 2)); // prints 2
return 0;
}
Is there an equivalent for VisualC++ that will work with zero arguments macros? Non standard extensions or tricks accepted.
EDIT: Fixed the example to work with GCC extensions and C++ compiler.
The following example works fine in VisualStudio 2010 and newer, gcc and clang with non standard extensions enabled. In Microsoft compilers it assumes the trailing comma in the AUGMENTER macro will be removed by the preprocessor when arguments count is zero. This is non standard and it has been also reported elsewere. In gcc and clang it uses the widely known ## __VA_ARGS__
non standard extension.
#include <stdio.h>
#ifdef _MSC_VER // Microsoft compilers
#define EXPAND(x) x
#define __NARGS(_1, _2, _3, _4, _5, VAL, ...) VAL
#define NARGS_1(...) EXPAND(__NARGS(__VA_ARGS__, 4, 3, 2, 1, 0))
#define AUGMENTER(...) unused, __VA_ARGS__
#define NARGS(...) NARGS_1(AUGMENTER(__VA_ARGS__))
#else // Others
#define NARGS(...) __NARGS(0, ## __VA_ARGS__, 5,4,3,2,1,0)
#define __NARGS(_0,_1,_2,_3,_4,_5,N,...) N
#endif
int main()
{
// NARGS
printf("%d\n", NARGS()); // Prints 0
printf("%d\n", NARGS(1)); // Prints 1
printf("%d\n", NARGS(1, 2)); // Prints 2
fflush(stdout);
#ifdef _MSC_VER
// NARGS minus 1
printf("\n");
printf("%d\n", NARGS_1(1)); // Prints 0
printf("%d\n", NARGS_1(1, 2)); // Prints 1
printf("%d\n", NARGS_1(1, 2, 3)); // Prints 2
#endif
return 0;
}
Macros were tested with real compilers, Wandbox and Webcompiler
Here is some code that works with gcc (5.3) AND VisualStudio2010 :
#include <stdio.h>
#define expand(x) x
#define prefix(...) 0,##__VA_ARGS__
#define lastof10(a,b,c,d,e,f,g,h,i,j,...) j
#define sub_nbarg(...) expand(lastof10(__VA_ARGS__,8,7,6,5,4,3,2,1,0))
#define nbarg(...) sub_nbarg(prefix(__VA_ARGS__))
#define test(...) printf("(%s) ---> %d\n",""#__VA_ARGS__,nbarg(__VA_ARGS__))
int main ()
{
test() ; // () ---> 0
test(1) ; // (1) ---> 1
test(1,2) ; // (1,2) ---> 2
test(1,2,3) ; // (1,2,3) ---> 3
test(1,2,3,4) ; // (1,2,3,4) ---> 4
test(1,2,3,4,5) ; // (1,2,3,4,5) ---> 5
test(1,2,3,4,5,6) ; // (1,2,3,4,5,6) ---> 6
return 0 ;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With