I got some example code to make a c++ variadic template from here:
http://en.wikipedia.org/wiki/Variadic_template
My code is as follows.
#ifdef DEBUG
#define logDebug(x, ...) streamPrintf( x, ##__VA_ARGS__ );
#else
#define logDebug(x, ...)
#endif
void streamPrintf(const char *s);
template<typename T, typename... Args>
void streamPrintf(const char *s, T value, Args... args)
{
while (*s) {
if (*s == '%') {
if (*(s + 1) == '%') {
++s;
}
else {
std::cout << value;
streamPrintf(s + 1, args...);
return;
}
}
std::cout << *s++;
}
throw std::logic_error("extra arguments provided to printf");
}
void streamPrintf(const char *s)
{
while (*s) {
if (*s == '%') {
if (*(s + 1) == '%') {
++s;
}
else {
throw std::runtime_error("invalid format string: missing arguments");
}
}
std::cout << *s++;
}
}
But it prints only junk. The main reason to use this is so I can print out std::string. How can I print out the correct values?
I call the function like this:
logDebug("Event is, event=%", value);
Peter T found the problem via chat. It does not print uint8_t correctly as it treats it like a ASCII. It needs to be type cast to e.g. uint16_t. When I have a solution, I will post it here.