Here is the code I'm not understanding
#include<iostream>
using namespace std;
template <typename T>
T calc(T, T) { cout << "template calc" << endl; }
double calc(double, double) { cout << "ordinary calc" << endl; }
template <>
char calc<char>(char, char) { cout << "template specialisation calc" << endl; }
int main() {
int ival;
double dval;
float fd;
calc(0, ival); // calls the generic calc(T, T)
// the following all call calc(double, double)
calc(0.25, dval);
calc(0, fd);
calc(0, 'J');
calc('I', 'J'); // calls calc(char, char)
}
There are 5 function calls to calc, so I will refer to them as as 1) - 5) depending on their position.
1) kind of makes sense. 0 is an integer, ival is an integer, it makes sense that calc(T, T) is called. Altho I feel like my reasoning on this is wrong. After all, they are both doubles, so if calc(double, double) were called, that would make sense too. So looking for clarification here.
2) no dramas, both are doubles, call calc(double, double). Simple.
3) fd is a float. Can either call calc(T, T) or calc(double, double). Because 1) resulted in a call to calc(T, T) I would assume that the same would hold here since we again have one param being 0, yet, this calls calc(double, double). So this confuses me, particularly the difference between this and 1)
4) My first thought was 0 is a valid character, so is 'J', so it calls calc(char, char). It might call calc(T, T) using a common type of integer. But nope, both wrong, it calls calc(double, double). Im very confused about this one. Makes no sense to me.
5) makes sense, but only if I apply the same logic as I did for 4), which was wrong in 4), so if I were to see another example of using templates I would not be sure as to what logic to apply.
So looking for explanation of why this program does what it does.
Thanks.