Normally, in C99, you can achieve function overloading (number of arguments, not type overloading) by using VA_ARGS and some kind of macro trick like:
#define THIRD_PARAMETER(_1,_2,_3,...) _3
#define NOTHING
for example:
void pr1(int x);
void pr2(int x, int y);
#define pr(...) THIRD_PARAMETER(__VA_ARGS__, pr2, pr1, NOTHING)(__VA_ARGS__)
(I add NOTHING
macro so that C99 won't complain about zero argument passed to ...
when I call pr(100)
to print 100
, I want my program is fully compatible with C99)
But the problem is: pr is not a function, so it can't be assigned to a function pointer inside a struct :
// this is a dynamic array
struct array {
// ...
void (*insert)(struct array * a, ...);
// ...
};
suppose i have 3 versions of insert: single_insert, multiple_insert, range_insert, which have 3,4,5 arguments respectively. How can I implement function overloading (number of arguments) inside a C99 struct ? is it possible ?