example19.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. #include <type_traits>
  2. #include <boost/safe_numerics/safe_integer.hpp>
  3. #include <boost/safe_numerics/safe_integer_range.hpp>
  4. #include <boost/safe_numerics/utility.hpp>
  5. using namespace boost::safe_numerics;
  6. void f(){
  7. safe_unsigned_range<7, 24> i;
  8. // since the range is included in [0,255], the underlying type of i
  9. // will be an unsigned char.
  10. i = 0; // throws out_of_range exception
  11. i = 9; // ok
  12. i *= 9; // throws out_of_range exception
  13. i = -1; // throws out_of_range exception
  14. std::uint8_t j = 4;
  15. auto k = i + j;
  16. // if either or both types are safe types, the result is a safe type
  17. // determined by promotion policy. In this instance
  18. // the range of i is [7, 24] and the range of j is [0,255].
  19. // so the type of k will be a safe type with a range of [7,279]
  20. static_assert(
  21. is_safe<decltype(k)>::value
  22. && std::numeric_limits<decltype(k)>::min() == 7
  23. && std::numeric_limits<decltype(k)>::max() == 279,
  24. "k is a safe range of [7,279]"
  25. );
  26. }
  27. int main(){}