I'm trying to overload a macro by the number of parameter.
Of course I can't actually overload the macro.
I've tried using variadic macros to choose the right macro (using the fact that if __VA_ARGS__
doesn't exist it's supposed to delete the last coma before it - GCC Reference):
#define TEST1() printf("TEST1");
#define TEST2() printf("TEST2");
#define CHOOSER(x, y,FUNC,...) FUNC()
#define MANIMACRO(...) CHOOSER(,__VA_ARGS__,TEST1,TEST2)
int main(void)
{
MANIMACRO(1);
MANIMACRO();
}
The idea was that if __VA_ARGS__
exists it should pass 4 arguments to CHOOSER, where the third one should have been "disappeared" with the unused args. so TEST1 would be chosen.
If there is no parameter the __VA_ARGS__
would be null, and should have removed the coma, so TEST2 would be chosen and used.
So, i guess that doesn't work because the __VA_ARGS__
probably gets removed only at the end of the preprocessing stage, after the whole thing was already expanded.
So, how can I do such a thing? (in vs2010)