+ 1

C++ - Templates - Where's the error?

Why doesn't this code function for "int" and "double"? #include <iostream> using namespace std; template <class T> T templating (T a, T b, T c){ T max = a; if(b > max){ max = b; } if(c > max){ max = c; } return max; }; int main() { cout << templating(1,4.7,3.9); return 0; }

13th Sep 2019, 5:40 PM
Paolo De Nictolis
Paolo De Nictolis - avatar
1 Answer
+ 5
All types are of type T so all arguments have to be of the same type. If you want to use different types you'd need to use something like T templating(U a, V b, W c)
13th Sep 2019, 5:47 PM
Aaron Eberhardt
Aaron Eberhardt - avatar