Function templates allow us to provide multiple templates type as shown in below example.
#include <iostream>#include <type_traits>template <typename T, typename U>auto min(T a, U b){ return a < b ? a : b;}int main(int argc, char const *argv[]){ std::cout << min(5, 4.2); return 0;}
We have used auto type deduction using auto keyword. This approach has one issue that the definition has to be there before the call (auto limitation). We can solve this issue by using common type as shown below.
#include <iostream>#include <type_traits>template <typename T, typename U>std::common_type_t<T, U> min(T a, U b);int main(int argc, char const *argv[]){ std::cout << min(5, 4.2); return 0;}template <typename T, typename U>std::common_type_t<T, U> min(T a, U b){ return a < b ? a : b;}