Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling template function without <>; type inference

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 explicitly 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 always have to specify at least 1 typename? I assume the reason is because C++ can not distinguish functions between different return types.

But if I use a void function and handover a reference, again I must not explicitly 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 explicitly writing the return type? Something like "type inference" for templates. Is "type inference" possible in C++0x?

like image 864
OlimilOops Avatar asked May 14 '10 11:05

OlimilOops


People also ask

How do you call a function in a template?

Defining a Function TemplateA function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.

Can a non-template class have a template member function?

A non-template class can have template member functions, if required. Notice the syntax. Unlike a member function for a template class, a template member function is just like a free template function but scoped to its containing class.

How can you define an implementation of a template when a specific type is passed as template parameter?

If we want to define a different implementation for a template when a specific type is passed as template parameter, we can declare a specialization of that template.

Do template functions need to be inline?

Yes, you need the inline specifier there. The ODR (one-definition rule) states that there must be exactly one definition of a variable, function, class, enum or template.


1 Answers

Overload resolution is done only based on function arguments; the return value is not used at all. If the return type cannot be determined based on the arguments, you will have to specify it explicitly.

I would not go down the path of "returning" a value through a reference parameter; that makes the calling code unclear. For example, I'd prefer this:

double x = round<double>(y);

over this:

double x;
round(x, y);

because in the latter case, it's easy to confuse input and output, and it's not at all clear that x is being modified.

In the particular case of round, you probably need only one or two types for TOut anyway, so you could just leave that template argument out:

template<typename TIn>
int roundToInt(TIn v) {
    return (int)(v + 0.5);
}

I find roundToInt(x) a little clearer than round<int>(x) because it's clear what the int type is used for.

like image 196
Thomas Avatar answered Sep 22 '22 07:09

Thomas