Home:ALL Converter>Overloading macros with variadic arguments

Overloading macros with variadic arguments

Ask Time:2015-03-19T06:48:59         Author:mic_e

Json Formatter

I'm trying to build a macro M which will expand to one of two possibilities, depeding on whether it has one, or more than one, arguments:

M(x)

should expand to

f(x)

While

M(x, "%d%d%d", 1, 2, 3)

should expand to

g(x, "%d%d%d", 1, 2, 3)

Where the function signatures are

f(int x);
g(int x, const char *fmt, ...);

There are various answers regarding the "overloading" of macros if the argument count is known; however their methods of determining the length of __VA_ARGS__ all work only to a finite, chosen number.

Is there any trick that might make a similar approach work for my "one argument / more than 1 arguments" case?

Note:

Overloading the functions is not an option because in my case they are actually constructors for two different classes.

Author:mic_e,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/29134152/overloading-macros-with-variadic-arguments
yy