Is there a better way to "overload" a macro like this? I need a macro that accepts various numbers of parameters.
#define DEBUG_TRACE_1(p1) std::string p[] = {p1}; log _log(__FUNCTION__, p, 1)
#define DEBUG_TRACE_2(p1, p2) std::string p[] = {p1, p2}; log _log(__FUNCTION__, p, 2)
#define DEBUG_TRACE_3(p1, p2, p3) std::string p[] = {p1, p2, p3}; log _log(__FUNCTION__, p, 3)
#define DEBUG_TRACE_4(p1, p2, p3, p4) std::string p[] = {p1, p2, p3, p4}; log _log(__FUNCTION__, p, 4)
#define DEBUG_TRACE_5(p1, p2, p3, p4, p5) std::string p[] = {p1, p2, p3, p4, p5}; log _log(__FUNCTION__, p, 5)
Called like this
DEBUG_TRACE_2("more", "params");
The easiest way to do your specific example would be with a variadic macro:
A couple notes:
__VA_ARGS__
is the name for the list of comma-separated arguments supplied to the macrosizeof
since p is a static arrayIf you need more flexibility than this, you can use a very neat trick to allow you to explicitly "overload" the macro to behave completely differently with a different number of parameters. However, this makes the code much more convoluted and should only be used if it is absolutely necessary. Since it seems like variadic arguments will do fine for your use case, I'll just provide a link: http://efesx.com/2010/07/17/variadic-macro-to-count-number-of-arguments/
It is possible to use the standard C /C++ variadic args in macros, at least in gcc (EDIT: apparently they are standardized, and MS c compiler also has them).
See this page for some information on how this works.
There is also another question on this site which may be helpful for you.