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