So, I looked at your question and I looked at your code and I realized that this is probably a case of the infamous XY Problem.
Although you say, you want to count the number of arguments being passed to the macro, what you want to do is write debug messages which do not have any other parameters except a literal string which states a debug message as you show in the example.
DEBUG(handler, DEBUG, "test");
But that is of course possible. And to test it, I wrote the following bit of code.
std::string format(const char* fmt, ...) {
char buffer[256];
std::cout << __FUNCTION__ << std::endl;
va_list argList;
va_start(argList, fmt);
vsprintf(buffer, fmt, argList);
va_end(argList);
return std::string(buffer);
}
#define TRC(...) {\
std::cout << "MACRO!" << std::endl; \
std::cout << format(__VA_ARGS__);\
}
int main()
{
std::cout << "Hello World!" << std::endl;
const char *t = "b";
TRC("[%s] bla bla [%d]\n", t, 9);
TRC("test");
return 0;
}
which produces the output,
Hello World!
MACRO!
format
[b] bla bla [9]
MACRO!
format
test
Like I mentioned in the comments to your question earlier, I have C++03
compiler but this should work for C++98 also, I guess.
Question:
"How can I check if there's only one argument or more than one?"
The variadic macro itself has no problem whether you have one argument or more.
But if you really really still want to count the number of arguments passed in, I don't know of anyway in C++03
or earlier to do that.