Suppose we've got the following two functions:
void foo1(int p);
void foo2(int p, ...);
I'd like to write a macro to automatically expand to the proper one based on the number of arguments. I've used the following dirty/hacky way, but I'm curious whether there's a clean solution for this problem or not.
#define SELECT(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, NAME, ...) NAME
#define foo(...) SELECT(__VA_ARGS__, \
foo2, foo2, foo2, foo2, \
foo2, foo2, foo2, foo2, \
foo2, foo2, foo2, foo1)(__VA_ARGS__)
This way only works if foo2
's number of arguments doesn't exceed 12. This is a drawback of my solution. I'm looking for a way without this limitation.
Update #1
The real problem: In Android NDK using the following functions we can write a log:
__android_log_print(int prio, const char *tag, const char *fmt, ...);
__android_log_write(int prio, const char *tag, const char *text);
To simplify the functions names, I define a macro called LOG
:
#define LOG(...) __android_log_print(0, "test", __VA_ARGS__)
If I pass the macro a string literal, it's okay, but when I pass a variable, compiler generates the warning -Wformat-security
. So, I'd like the macro calls with single argument to expand to __android_log_write
and others to __android_log_print
. My use cases for log: 1. string literal with/without arguments 2. single argument variable char *
.