So, basically I'm trying to implement a macro to count the number of arguments in VA_ARGS.
For the sake of simplicity it only works up to 3 parameters. The problem is that when the macro is used with less than 3 parameters, it doesn't work, and triggers the "expected an expression" error.
#define EXPAND( x ) x
#define PP_NARG(...) EXPAND(PP_ARG_N(__VA_ARGS__, PP_RSEQ_N()))
#define PP_ARG_N(_1, _2, _3, N,...) N
#define PP_RSEQ_N() 3,2,1,0
void main()
{
printf("\nTEST PP_NARG: %i", PP_NARG()); //Doesn't work (in this case it shouldn't work, so it's correct)
printf("\nTEST PP_NARG: %i", PP_NARG(0)); //Doesn't work
printf("\nTEST PP_NARG: %i", PP_NARG(0,0)); //Doesn't work
printf("\nTEST PP_NARG: %i", PP_NARG(0,0,0)); //Works
}
Keeping just the line that works it compiles correctly and prints "TEST PP_NARG: 3".
I believe the problem might be that PP_RSEQ_N() is only expanding to "3", instead of "3,2,1,0" for some reason, since even if PP_RSEQ_N() is defined as this
#define PP_RSEQ_N() 10,9,8,7,6,5,4,3,2,1,0
it still doesn't work with less than 3 parameters.
Im using the MSVC compiler and it may be the cause of the problem, since it doesn't behave very well with macros, as seen here: MSVC doesn't expand __VA_ARGS__ correctly