I am wanting to make a template function that adds three numbers. The type may be int or char or string. How can I add these then return the correct value using the same type. Example: three strings of numbers {5,6,7} should add up to 18 then return 18 as a string. three chars of numbers {5,6,7} should add up to 18 then return 18 as a char.
template <class MyType>
MyType GetSum (MyType a, MyType b, MyType c) {
return (a+b+c);
}
int a = 5, b = 6, c = 7, d;
char e = '5', f = '6', g = '7', h;
string i= "5", j= "6", k= "7", l;
d=GetSum<int>(a,b,c);
cout << d << endl;
h=GetSum<char>(e,f,g);
cout << h << endl;
l=GetSum<string>(i,j,k);
cout << l << endl;
This code works for int but obviously not for char or string. I am not sure how to convert from an unknown type to int and back so i can add the numbers.