Say I have a function like this defined in a C++ header:
namespace foo {
void bar(int a, int b = 1);
}
and I would like to use this function in C code. One obvious solution would be to define two functions like this:
void foo_bar_1(int a)
{ foo::bar(a, 1); }
void foo_bar_2(int a, int b)
{ foo::bar(a, b); }
These can then easily be included in C code. However, this gets ugly for multiple default parameters, it would be nicer to have a single wrapper function. I thought about doing something like this:
#define _test_foo_numargs(...) (sizeof((int[]){__VA_ARGS__})/sizeof(int))
#define test_foo(...) do { \
if (_test_foo_numargs(__VA_ARGS__) == 1) \
test_foo_1(__VA_ARGS__); \
else if (_test_foo_numargs(__VA_ARGS__) == 2) \
test_foo_2(__VA_ARGS__); \
} while (0)
But that doesn't work because both of the calls to test_foo_1
and test_foo_2
have to be valid in order for this to compile.
Is there a better way to do this?