I'm familiar with the following way of creating a macro with variable number of arguments. However, consider:
#define MY_MACRO_N(value, format, ...) my_func(value, format, ##__VA_ARGS__)
#define MY_MACRO_0(value) my_func(value, NULL)
Where my_func
takes variable number of arguments as well. How can I create a MY_MACRO
macro that encapsulates both, such that:
MY_MACRO(my_value); // expand to my_func(my_value, NULL);
MY_MACRO(my_value, my_format); // expand to my_func(my_value, my_format);
MY_MACRO(my_value, my_format, a, b); // expand to my_func(my_value, my_format, a, b);
MY_MACRO(); // invalid
The variable number of arguments break the GET_MACRO
approach, however it seems like there should be a way to do this.
my_func
has no upper bound.printf
-like syntax (and it looks like it) and if you are willing to accept an empty string as default instead ofNULL
and if you are okay with restricting the format of literal strings if it is given, you can make use of string-literal concatenation. In this special, but common case, the macro looks like#define LOG(CODE, ...) my_func(CODE, "" __VA_ARGS__)
. (But the format checker will complain about empty format strings, so that may not be a good solution.)LOG(X)
expands tomy_func(X, "")
, anything else tomy_func(X, "" "<fmt>", ...)
, which will paste the empty string to your format string, see here. That will only work if your format string is a literal, but that's usually the case. Never mind. It was just a suggestion that you might have found useful.