A way in C++ to make functions polymorphic
template <typename T> // <...> can also be written <class T>
int compare (const T &value1, const T &value2){
if (value1 < value2) return -1;
if (value2 < value1) return 1;
return 0;
}
int main (int argc, char **argv){
int main(int argc, char **argv) {
std::string h("hello"), w("world");
std::cout << compare(10, 20) << std::endl; // ok
std::cout << compare(h, w) << std::endl; // ok
std::cout << compare("Hello", "World") << std::endl; // hm...
return EXIT_SUCCESS;}
}
The compiler doesn’t generate any code when it sees the template function
Pro of template:
Generate pretty efficient code, no penalties using a template.
This also work

Because the main function need to now the whole body of comp, you can not just make the prototype in the .h file.