Problem with very simple variadic function

Viewed 97

I am trying to use a very simple log function using variadic templates in C++:

void log(){}

template <typename T, typename... Types>
void log(T first, Types... arg)
{
    std::cout << first << " ";
    log(arg...);
}

int main()
{
    log(1,2);
    log(3, "four");
    log(5);
    log(6,"seven",8,9,10,11,12);
    log(13,14);

}

But in the output I am missing all the last arguments of the log function if the are integers (2, 5, 12 and 14) but not if they are strings ("four") !??. Why is that? What I am doing wrong?

output: 1 3 four 6 seven 8 9 10 11 13
1 Answers

Mystery solved - when compiling with MSVC your choice of the name log is clashing with the log function in the standard library when there is only one numeric parameter. Since this doesn't print anything, output is missing.

Just use another name and it works.

Related