hopangtsun@gmail.com wrote:[color=blue]
> Hi all, I have encountered a problem on using the function template
> my goal is to add two numbers, which they can be int or double if I
> do this in this way template<class T> T addition(T a, T b){
> return (a + b); } it can only deal with int + int or double +
> double however, I also want int + double, double + int so I come
> up with this idea template<class T , class U> T addition(T a, U b){
> return (a+b); } this created a problem that it will return the type
> of the first parameter for example: int + double would return an
> int!!!(incorrect) 2 + 3.3 = 5 X double + int would return a
> double!!!(correct) 3.3 + 2 = 5.3 so I would like to know is there any
> methods that it can return the type correctly using the function
> template?[/color]
I think you need some kind of type traits for that. Or you could supply
the third type to be used as the return value type:
template<class R, class T, class U> R addition(T a, U b) {
return a + b;
}
...
addition<float>(1, 'b');
V
--
Please remove capital As from my address when replying by mail