I made a template and an auto function that compare 2 values and return the smallest one. This is my code:
#include <iostream>
using namespace std;
// Template with a value returning function: PrintSmaller
template <typename T, typename U>
auto PrintSmaller(T NumOne, U NumTwo) {
if (NumOne > NumTwo) {
return NumTwo;
}
else {
return NumOne;
}
}
int main() {
int iA = 345;
float fB = 23.4243;
cout << PrintSmaller(iA, fB) << endl;
cout << PrintSmaller(fB, iA) << endl;
return 0;
}
But it won't compile, I get this error on VS 2015: Error C3487 'int': all return expressions must deduce to the same type: previously it was 'float'
However, if I delete the if statement and write the function PrintSmaller like this it works with no problems :
auto PrintSmaller(T NumOne, U NumTwo) {
return (NumOne < NumTwo ? NumOne : NumTwo);
}
What is the difference ? and why the first code won't compile ? Thank you.