Using Varaidic micros with Microsoft VS2010

I was trying to use the variadic micros with Microsoft visual studio 2010 and I faced a problem in the preprocessing phase , the complier couldn't translate the expressions correctly.

I searched on the internet for a solution for this problem and I found out that there is some bug in MSV2010 which cause this problem, some programmers suggested workaround methods to solve this problem and actually they solved it "http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/#comment-742"
but I am still facing small problem.

Here you are the code I am using.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>

#define VA_NUM_ARGS(…) VA_NUM_ARGS_IMPL_((__VA_ARGS__, 5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_1,_2,_3,_4,_5,N,…) N


#define macro_dispatcher(func, ...)   macro_dispatcher_(func, VA_NUM_ARGS(__VA_ARGS__))

#define macro_dispatcher_(func, nargs)           macro_dispatcher__(func, nargs)
#define macro_dispatcher__(func, nargs)          func ## nargs

// to verify, run the preprocessor alone (g++ -E):
//VA_NUM_ARGS(x,y,z)


#define max(...)	 macro_dispatcher(max, __VA_ARGS__)(__VA_ARGS__)

#define max1(a)		 a
#define max2(a,b)	 ((a)>(b)?(a):(b))
#define max3(a,b,c)	 max2(max2(a,b),c)
int main(void)
{
	int a,b,c,d,z;
	a=10;
	b=6;
	c=5;
	
	d=VA_NUM_ARGS(a,b,c);
	z=max(a,b,c);
	printf("Number of Arguments=%d and the max number=%d",d,z);


}


d=VA_NUM_ARGS(a,b,c) ... Here the code will work correctly and gives the expected data which is d=3;

but for z=max(a,b,c); .... the macros will not be translated correctly and the program will show an error

I have run the preprocessor operation and this is what I got as translation for
z=max(a,b,c)"before preprocessor"
z=maxVA_NUM_ARGS_IMPL (a,b,c, 5,4,3,2,1) (a,b,c); "after preprocessor" which isn't the expected as the expcted is
z=max3(a,b,c);

How can I get the correct answer?
Thanks for you help.
Topic archived. No new replies allowed.