Consider the following program and guess the results:
#include <iostream>
template <typename T>
void print(const T &value)
{
static int times{0};
std::cout << ++times << ": " << value << '\n';
}
int main(int argc, char const *argv[])
{
print(1);
print(3);
print(5);
print(1.2);
print(1.4);
print(4.1);
return 0;
}Surprisingly, it must give something like below:
1: 1
2: 3
3: 5
1: 1.2
2: 1.4
3: 4.1
This happens because when print is called with different datatype arguments, different function template instantiation happens with their independent static variables.