I've been looking at templates where a set of instructions is carried out on any one type of data and I get the gist of it and all but I couldnt help but wonder - why don't programming languages implement generics like a regular type. I mean instead of :
1 2 3 4 5 6
Template <class T>
T returnBigger(T firstValue, T secondValue){
//
}
Can't we have a generic type 'type' (like int or float e.t.c ) that we can just use naturally. So the above code would be
1 2 3 4 5
type returnBigger(type firstValue, type secondValue){
//
}
Isn't this possible? Forgive me of this question is insanely dumb.
Because then:
1. Depending on what your declaration means, it is more restrictive or more permissive than a template:
1 2 3 4
//type returnBigger(type firstValue, type secondValue); is equivalent to
template <typename T1,typename T2,typename T3>
T3 returnBigger(T1 firstValue, T2 secondValue);
//Note: No way to force the parameters to be of the same type.
or:
1 2 3 4
//type returnBigger(type firstValue, type secondValue); is equivalent to
template <typename T>
T returnBigger(T firstValue, T secondValue);
//Note: No way to allow the parameters to be of different types.
Your syntax, while more comfortable in some situations, in many more others would be much less powerful.
2. You can't pass integers as template parameters: