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[]
The easiest way to do your specific example would be with a variadic macro:
#define DEBUG_TRACE(...) \
do { \
std::string p[] = { __VA_ARGS__ }; \
log _log(__FUNCTION__, p, (sizeof p) / (sizeof p[0])); \
} while (0)
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/