Using template specialization in C++

Posted by user550413 on Stack Overflow See other posts from Stack Overflow or by user550413
Published on 2011-01-09T19:49:22Z Indexed on 2011/01/09 23:53 UTC
Read the original article Hit count: 143

Filed under:
|

How can I write a function using template specialization that has 2 different input types and an output type:

template <class input1, class input2, class output>

and return the sum of the 2 numbers (integers/doubles). However, if I get 2 integers I want to return an integer type but for any other combinations of integer and double I'll always return double.

I am trying to do that without using directly the '+' operator but having the next functions instead:

double add_double_double(double a, double b) {return (a+b);}
double add_int_double(int a, double b) {return ((double)(a)+b);}
int   add_int_int(int a, int b) {return (a+b);}

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates