I am attempting to rewrite old C code in its C++ equivalent. The module i am working on seem to use C variable arguments list alot. One of the common usage example is as follows (PLEASE NOTE THAT BELOW CODE IS JUST INDICATIVE EXAMPLE, its not actual production code. MY INTENTION IS TO REWRITE THE USAGE OF VARARGS with Modern C++ constructs) :
#include <stdio.h>
#include <stdarg.h>
void simple_printf(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
while (*fmt != '\0') {
if (*fmt == 'd') {
int i = va_arg(args, int);
printf("%d\n", i);
} else if (*fmt == 'c') {
// A 'char' variable will be promoted to 'int'
// A character literal in C is already 'int' by itself
int c = va_arg(args, int);
printf("%c\n", c);
} else if (*fmt == 'f') {
double d = va_arg(args, double);
printf("%f\n", d);
}
++fmt;
}
va_end(args);
}
int main(void)
{
simple_printf("dcff", 3, 'a', 1.999, 42.5);
}
I am interested in knowing what are the alternatives available in Modern C++ to rewrite this code something similar as above. I am also interested in knowing multiple options available in Modern C++. A code example will be more beneficial.