Bounded Signed Integer Types
Description
bounded_int<Min, Max> is a compile-time bounded signed integer type that enforces value constraints at both compile time and runtime.
The bounds Min and Max are non-type template parameters of any signed integer type (including the library’s i8, i16, etc.).
The underlying storage type (basis_type) is automatically selected as the smallest signed safe integer type that can represent the full [Min, Max] range:
| Condition | Basis Type |
|---|---|
Both bounds fit in |
|
Both bounds fit in |
|
Both bounds fit in |
|
Both bounds fit in |
|
Otherwise |
|
Synopsis
#include <boost/safe_numbers/bounded_integers.hpp>
namespace boost::safe_numbers {
template <auto Min, auto Max>
requires (valid_signed_bound<decltype(Min)> &&
valid_signed_bound<decltype(Max)> &&
signed_raw_value(Max) > signed_raw_value(Min))
class bounded_int {
public:
using basis_type = /* automatically selected */;
// Construction (throws std::domain_error if out of range)
explicit constexpr bounded_int(basis_type val);
explicit constexpr bounded_int(underlying_type val);
// Conversions
template <SignedType T>
explicit constexpr operator T() const;
template <auto Min2, auto Max2>
explicit constexpr operator bounded_int<Min2, Max2>() const;
explicit constexpr operator basis_type() const noexcept;
explicit constexpr operator underlying_type() const noexcept;
// Comparison (defaulted three-way)
friend constexpr auto operator<=>(bounded_int, bounded_int) noexcept -> std::strong_ordering = default;
// Unary operators
constexpr auto operator+() const noexcept -> bounded_int;
constexpr auto operator-() const -> bounded_int; // throws on min negation or out-of-bounds
// Arithmetic operators (throw on overflow/underflow/out-of-range)
friend constexpr auto operator+(bounded_int, bounded_int) -> bounded_int;
friend constexpr auto operator-(bounded_int, bounded_int) -> bounded_int;
friend constexpr auto operator*(bounded_int, bounded_int) -> bounded_int;
friend constexpr auto operator/(bounded_int, bounded_int) -> bounded_int;
friend constexpr auto operator%(bounded_int, bounded_int) -> bounded_int;
// Compound assignment
constexpr auto operator+=(bounded_int) -> bounded_int&;
constexpr auto operator-=(bounded_int) -> bounded_int&;
constexpr auto operator*=(bounded_int) -> bounded_int&;
constexpr auto operator/=(bounded_int) -> bounded_int&;
// Increment / Decrement
constexpr auto operator++() -> bounded_int&;
constexpr auto operator++(int) -> bounded_int;
constexpr auto operator--() -> bounded_int&;
constexpr auto operator--(int) -> bounded_int;
};
} // namespace boost::safe_numbers
Exception Behavior
| Condition | Exception Type |
|---|---|
Value outside |
|
Signed addition overflow |
|
Signed addition underflow |
|
Signed subtraction overflow |
|
Signed subtraction underflow |
|
Signed multiplication overflow |
|
Signed multiplication underflow |
|
Division by zero |
|
Division |
|
Modulo by zero |
|
Modulo |
|
Negation of type minimum |
|
Negation result outside bounds |
|
Increment result exceeds Max |
|
Decrement result below Min |
|
Domain Assumptions on Conversion
As for bounded_uint, every conversion out of a bounded_int (to a basis type, to an underlying type, or to another bounded_int) hands the declared domain to the optimizer as an assumption, so checks on the receiving type that the domain makes impossible are deleted.
The checks of the bounded type itself are unaffected.