If I have one template class and template function like this
template <class T> T getMax (T a, T b) {
return (a>b?a:b);
}
template <class T> class GetMax {
public:
static T getMax(T a, T b) {
return (a>b?a:b);
}
};
Why are these not valid?
x=getMax(1, '2');
but these are valid
x=getMax(1,2);
Does it mean that there is no type conversion in template function?
This is not valid
x=GetMax::getMax(1, 2);
Does it mean that for the template class, the type must be specified?
What should
getMax(1, '2');return? An int, or a char? Think about it 🙂You could write:
But note that you are explicitly returning type 1, what might not work in a case like
getMax('1',1000)because 100 would be converted to char type, and that wouldn’t be large enough.The latter is not valid because to use a class, you must first state what type it is — this mechanism acts first, before type deduction.
It would work if you stated it :