After a bit of research and a lot of mucking about I've come up with this:
#include <stdio.h>
// Variadic Function Overloading:
#define VOID "__variadic_VOID__"
#define variadic_count(...) variadic_count_(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)
#define variadic_count_(_6, _5, _4, _3, _2, _1, count, ...) count
#define variadic_token(func, ...) variadic_token_(func, variadic_count(__VA_ARGS__))
#define variadic_token_(func, count) variadic_token__(func, count)
#define variadic_token__(func, count) func ## _ ## count
#define variadic(func, ...)\
do {\
if (#__VA_ARGS__ == "\"__variadic_VOID__\"")\
{\
variadic_token__(func, 0)();\
}\
else\
{\
variadic_token(func, __VA_ARGS__)(__VA_ARGS__);\
}\
} while (0)
// Usage:
#define somefunction(...) variadic(somefunction, __VA_ARGS__)
#define somefunction_0()\
do {\
printf("somefunction_0(VOID)\n\n");\
} while (0)
#define somefunction_1(x)\
do {\
printf("somefunction_1(x = %i)\n\n", (x));\
} while (0)
#define somefunction_2(x, y)\
do {\
printf("somefunction_2(x = %i, y = %i)\n\n", (x), (y));\
} while (0)
int main(int argc, char* argv[])
{
//somefunction(); ERROR
somefunction(VOID);
somefunction(1);
somefunction(11);
somefunction(2, 3);
//somefunction(1, 2, 3); ERROR
printf("\n\n");
return 0;
}
Essentially, it allows for a distinction between zero and one arguments via the special token VOID. It works unless the variadic function/macro is passed the string literal defined by VOID as a sole argument. In this case, only a call of somefunction("__variadic_VOID__"); will cause unexpected behavior; passing a variable with the same value as the string literal does not cause unexpected behavior.
While the provided code only works for 0-6 arguments, it can be modified to work with a greater number of arguments.
I'm curious, however, as to whether or not if ("blah" == "blah") {doSomething();} is optimized to doSomething(); by the compiler? Or does the pointer comparison occur at runtime? If it is optimized away, then I think this code snippet effectively allows for easy variadic overloading of functions/macros with virtually no downside...