Possible Duplicate:
Template deduction for function based on its return type?
I am trying to figure out how C++ templates work. As an example, I want to write a simple data structure, triple which behaves like STL’s pair but has three items. Of course, I want to implement it using templates. And I am facing a strange behaviour from my point of view.
First, I have a working example. Here is the C++ code that works:
template<typename T1, typename T2>
T1 getSum(T1 a1, T2 a2) {
return a1 + a2;
}
int main(int argc, int *argv[]) {
long l1 = 1, l2 = 0, l3 = 0, l4 =0;
short s1 = 2, s2 = 0, s3 = 0, s4 = 0;
l2 = getSum(l1, s1);
l3 = getSum(s1, l1);
l4 = getSum(1, 2);
s2 = getSum(l1, s1);
s3 = getSum(s1, l1);
s4 = getSum(1, 2);
cout << l2 << " " << l3 << " " << l4 << endl;
cout << s2 << " " << s3 << " " << s4 << endl;
return 0;
}
The result of this program is two lines containing 3 3 3 each. Since I call getSum function with various combination of arguments: (long, short), (short, long) and (integer literal, integer literal) and the return value is assigned into both long and short variables each time, I assume that compiler knows how to make an appropriate conversion or guessing of types by return values (in case of integer literals) each time.
And here we come back to simple triple implementation. I do everything in pretty the same way (as it seems to me) but I get a conversion error. Here is what I mean. My code looks like the following:
template <typename T1, typename T2, typename T3>
struct triple {
T1 first;
T2 second;
T3 third;
};
template <typename T1, typename T2, typename T3>
triple<T1, T2, T3> mt(T1 f, T2 s, T3 t) {
triple<T1, T2, T3> tr;
tr.first = f;
tr.second = s;
tr.third = t;
return tr;
};
int main(int argc, char* argv[]) {
triple<char, char, unsigned short> t = mt(0, 0, 65000);
cout << t.first << " " << t.second << " " << t.third << endl;
return 0;
}
However, when I try to compile it, I get the following error:
22: error: conversion from 'triple<int, int, int>' to non-scalar type
'triple<char, char, short unsigned int>' requested
(22nd line is the first line of main function).
It is not surprise that conversion between two aforementioned triples is not a compiler’s business. But it is a surprise for me that compiler cannot determine that since I want to get triple<char, char, short unsigned int> as a result of mt function, template arguments should be determined as T1 == char, T2 == char, T3 == unsigned short.
And since things work this way now, I’m confused about how make_pair function works. Its code looks very alike to mine (again, as it seems to me).
So, my question is: please, clarify the essential differences between my code and working one 🙂
The problem in your case is that your function
mthas a fixed return type already:Triple<int, int, int>(determined by the parameters, and integer constants are always considered to beintunless specified otherwise). Once created that way, it can’t be assigned, since it’s a different type, and there is no type conversion or constructor defined which would allow what you’re trying to do.At the moment, there’s no constructor at all explicitly defined in your
triplestrucht – which means there’s only default constructor and the copy constructor (which works only for the exact same type)!But there is a solution:Take a look at the definition of pair: See the copy constructor (which is templated again, in addition to the template parameters on the base class). To make it work for your case, add your own templated copy constructor:
Instead of using a function returning a triple, you could also simply use another constructor taking the three parameters:
and then in main you could write:
It’s also less to write!