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);
}
|