Hi,
if I have a function template with typename T, where the compiler can set the type by itself, I do not have to write the type explicitely when I call the function like:
template < typename T >
T min( T v1, T v2 ) {
return ( v1 < v2 ) ? v1: v2;
}
int i1 = 1, i2 = 2; int i3 = min( i1, i2 ); //no explicit <type>
but if I have a function template with two different typenames like...
template < typename TOut, typename TIn >
TOut round( TIn v ) {
return (TOut)( v + 0.5 );
}
double d = 1.54;
int i = round<int>(d); //explicit <int>
Is it true that I have to specify at least 1 typename, always? I assume the reason is because C++ can not distinguish functions between different return types, true? but if I use a void function and handover a reference, again I must not explicitely specify the return typename:
template < typename TOut, typename TIn >
void round( TOut & vret, TIn vin ) {
vret = (TOut)(vin + 0.5);
}
double d = 1.54;
int i; round(i, d); //no explicit <int>
should the conclusion be to avoid functions with return and more prefer void functions that return via a reference when writing templates? Or is there a possibility to avoid explicitely writing the return type? something like "type inference" for templates... is "type inference" possible in C++0x?
I hope I was not too unclear.
many thanks in advance
Oops