Let's say I recreate printfusing the options package:
void printf(const char* string)
{
std::cout << string;
}
template <typename T, typename... Params>
void printf(const char* string, T first, Params... p)
{
while (*string != '\0')
{
if (*string == '%' && *(string + 1) != '%')
{
std::cout << first;
string += 2;
printf(string, p...);
return;
}
std::cout << *string++;
}
}
I call a function with the following parameters:
printf("%d is the answer to life, the universe, and everything. Pi = %f", 42, 3.14);
Will the compiler create two different functions with different signatures?
printf(const char*, int, Params...);
and printf(const char*, double, Params...);
If so, calling a function with 10 arguments will create 10 different functions. Does this compiler optimize at all?
source
share