Subi Suresh
Subi Suresh

Reputation: 301

Explicit template argument specification

I started reading about templates and I got confused on the below.

template<class T>
T max(T t1, T t2)
{
   if (t1 > t2)
      return t1;
   return t2;
}

int main(){
   std::cout<<max<int>(120,14.55);
   return 0;
}

Output is 120. But when I compiled the above I got this warning:

warning:passing double for argument 2 to T max(T, T) [with T = int].

My question is why this warning came, since I have already instantiated for T max(int t1,double t2).

Because as per my understanding here if I explicitly mention only one data type (here int), other would be deducted from the argument type (14.55) by the compiler. That means T max(T t1, T t2) instantiates T max(int t1,double t2) in this case. I read this concept from some template documents on Internet.

Please clear my doubts else I can't proceed further.

Upvotes: 2

Views: 3082

Answers (3)

Mike Seymour
Mike Seymour

Reputation: 254501

why this warning came is my question since i have already instantiated for T max(int t1,double t2).

No you haven't. Providing int as the template parameter instantiates the template by replacing all occurences of the parameter with int:

T max(T t1, T t2)    becomes    int max(int t1, int t2)

Because as per my understanding here if i explicitly mention only one data type (here int), other would be deducted from the argument type

No. Your template has only one parameter, used for both function argument types; so if you specify that parameter, then it's used for both (and the return type as well). If you want these to be parametrised separately, then you'll need a separate parameter for each.

template <typename T1, typename T2>
???? max(T1 t1, T2 t2)

How best to specify the return type is left as an exercise.

Now, if you only provide the first argument, the second is deduced from the second function argument:

max<int>(120,14.55);  // T1=int given, T2=double deduced -> max(int,double)
max(120,14.55)        // T1=int, T2=double both deduced  -> max(int,double)

Upvotes: 0

Arsenii Fomin
Arsenii Fomin

Reputation: 3336

std::cout << max<int>(120,14.55);

This line is equavivalent to using of function int max(int, int), so compiler gives warning for conversion from 14.55 to 14 - possible loss of data.

If you want to compare diffrent types of data, you should use

template<class T, class T1, class T2>
T max(T1 t1, T2 t2)
{
   if (t1 > t2)
      return T(t1);
   return T(t2);
}

later in main.cpp:

float a = max<float, int, double>(10, 11.0);

but it's not the best way to do it.

Upvotes: 1

Your particular template only has one template parameter, T. Which means that max<int> creates this:

int max(int t1, int t2)

There is no way to generate max(int, double) out of this template. If you want mixed-type arguments, the template would have to look like this:

template <typename T1, typename T2>
/*some return type*/ max(T1 t1, T2 t2)

However, now it's not easy to determine what the return type should be, and type_traits would probably be needed to derive a correct return type for such a function.

Upvotes: 2

Related Questions