fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <stdexcept>
  5.  
  6. template <typename MyType> int ToInt (MyType);
  7. template <> int ToInt<int> (int x) { return x; }
  8. template <> int ToInt<std::string> (std::string x) { return std::stoi(x); }
  9. template <> int ToInt<char> (char x) { return std::stoi(std::string(&x, 1)); }
  10.  
  11. template <typename MyType> MyType FromInt (int);
  12. template <> int FromInt<int> (int x) { return x; }
  13. template <> std::string FromInt<std::string> (int x) {
  14. std::ostringstream oss;
  15. oss << x;
  16. return oss.str();
  17. }
  18. template <> char FromInt<char> (int x) {
  19. static const std::string map("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  20. return map.at(x);
  21. }
  22.  
  23. template <typename MyType>
  24. MyType GetSum (MyType a, MyType b, MyType c) {
  25. int aa = ToInt(a);
  26. int bb = ToInt(b);
  27. int cc = ToInt(c);
  28. return FromInt<MyType>(aa + bb + cc);
  29. }
  30.  
  31. int main () {
  32. int a = 5, b = 6, c = 7, d;
  33. char e = '5', f = '6', g = '7', h;
  34. std::string i= "5", j= "6", k= "7", l;
  35.  
  36. d=GetSum(a,b,c);
  37. std::cout << d << std::endl;
  38.  
  39. h=GetSum(e,f,g);
  40. std::cout << h << std::endl;
  41.  
  42. l=GetSum(i,j,k);
  43. std::cout << l << std::endl;
  44. }
  45.  
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
18
I
18