I was going through a textbook and found a question regarding templates.
Q. Rewrite this function using templates to work with any type. List the operations that any type using this template function must support
int FindLargest(const int& a, const int& b) { int ret; if (a > b) { ret = a; } else { ret = b; } return ret; } My answer:
template<typename T> T FindLargest(const T& a, const T& b) { T ret; if (a > b) { ret = a; } else { ret = b; } return ret; } It was easy to answer first part(rewrite using template..) but I am confused with the second sentence of the question. what is it trying to say?
aandb?FindLargest, how would they go about verifying that the type they pass is valid? (Without actually calling it and watching for compiler errors.) This information is present in high-quality documentation of the function.FindLargestwork if I passed it two output streams likecout? Why or why not?aandbhave different types (e.g.FindLargest(1.8, 5))?