Does the recursive function of the set of template parameters create a specific function for each iteration?

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?

+4
source share
1 answer

I expect the following two instances of the template to be obtained from this call:

printf(const char*, int, double);

and

printf(const char*, double);

, printf(), . , , .

,

printf("Foo %d\n", 10);

printf("Bar %d\n", 20);

, . .

+4

All Articles