fork download
  1. #include <iostream>
  2. #include <limits>
  3.  
  4. #define WRAPPER(Name, defaultValue, maxValue) \
  5. template <class T> \
  6. struct Name { \
  7.   T value; \
  8.   constexpr inline Name(const T& value = defaultValue) : value(value) {} \
  9.   explicit inline operator T() const { return value; } \
  10.   explicit inline operator bool() const { return value != defaultValue; }
  11.  
  12. WRAPPER(Assign, -std::numeric_limits<T>::max(), std::numeric_limits<T>::max())
  13. inline void operator += (const Assign& other) {
  14. if (other) value = other.value;
  15. }
  16. friend inline Assign operator + (const Assign& lhs, const Assign& rhs) {
  17. return rhs.value;
  18. }
  19. };
  20.  
  21. WRAPPER(Sum, 0, std::numeric_limits<T>::max() / 2)
  22. inline void operator += (const Sum& other) { value += other.value; }
  23. inline void operator += (const Assign<T>& other) {
  24. if (other) value = other.value;
  25. }
  26. friend inline Sum operator + (const Sum& lhs, const Sum& rhs) {
  27. return lhs.value + rhs.value;
  28. }
  29. };
  30.  
  31. int main() {
  32. Sum<int> sum(2);
  33. sum += Sum<int>(3);
  34. std::cout << (int)sum << std::endl;
  35. sum += Assign<int>(4);
  36. std::cout << (int)sum << std::endl;
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0s 16048KB
stdin
Standard input is empty
stdout
5
4