fork download
  1. #include <cstdint>
  2.  
  3. #include <iostream>
  4. #include <limits>
  5.  
  6. #define define_result_type( t1, t2, r )\
  7. template <>\
  8. struct addition_result<t1,t2> {\
  9.   typedef r type;\
  10. }
  11.  
  12. template <typename T1, typename T2>
  13. struct addition_result;
  14.  
  15. define_result_type(int8_t, int8_t, int16_t);
  16. define_result_type(int16_t, int16_t, int32_t);
  17. define_result_type(int32_t, int32_t, int64_t);
  18.  
  19. template<typename T1, typename T2>
  20. typename addition_result<T1,T2>::type
  21. add( T1 const & lhs, T2 const & rhs ) {
  22. typedef typename addition_result<T1,T2>::type result_type;
  23.  
  24. return result_type(lhs) + result_type(rhs);
  25. };
  26.  
  27. int main() {
  28. using namespace std;
  29.  
  30. int32_t a, b;
  31. a = b = numeric_limits<decltype(a)>::max();
  32.  
  33. cout << a + b << endl;
  34. cout << add(a, b) << endl;
  35. }
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
-2
4294967294