Class Subspace :: sus :: num :: OverflowInteger

template <class I>
class OverflowInteger
requires
sus::num::Integer<I>
{ ... };

An integer type that handles overflow instead of panicing.

The value inside the integer can be accessed or unwrapped like with an Option, which will panic if the integer has overflowed. Or it can be converted into an Option that will represent the overflow state as None.

This type is useful for performing a series of operations as a unit, and then checking for overflow after. It satisfies the Sum and Product concepts so can be used with sum and product for iterators over integers.

Examples

Using OverflowInteger to sum an iterator of integers and look for overflow after without panicking.

auto a = sus::Array<i32, 2>(2, i32::MAX);
auto maybe_answer =
    a.iter().copied().product<sus::num::OverflowInteger<i32>>();
sus_check(maybe_answer.is_overflow());

Static Methods

OverflowInteger()
requires
sus::construct::Default<I>
OverflowInteger(std::convertible_to<I> auto u)

Constructs an OverflowInteger from the same subspace integer type.

Implementation note

Because OverflowInteger is constructible from a smaller integer, but I is also constructible from a smaller integer, which can then be used to construct OverflowInteger, on MSVC this conversion becomes ambiguous. So we use convertible_to to make the constructor a template, which allows the compiler to choose one. https://developercommunity.visualstudio.com/t/Ambiguous-conversion-with-two-paths-of-d/10461863

template <class U>
static auto from(U u) -> OverflowInteger<I>
requires
std::constructible_from<OverflowInteger<I>, U>

Satisfies sus::construct::From<OverflowInteger<I>, U> if the OverflowInteger is constructible from U.

static auto from_product(Iterator<I> auto&& it) -> OverflowInteger<I>
requires
sus::mem::IsMoveRef<decltype(it)>
static auto from_product(Iterator<OverflowInteger<I>> auto&& it) -> OverflowInteger<I>
requires
sus::mem::IsMoveRef<decltype(it)>

Constructs an OverflowInteger from an Iterator by computing the product of all elements in the iterator.

This method should rarely be called directly, as it is used to satisfy the sus::iter::Product concept.

This method satisfies sus::iter::Product<OverflowInteger<T>, T> as well as sus::iter::Product<OverflowInteger<T>, OverflowInteger<T>>, for a subspace integer type T.

If an iterator yields a subspace integer type, iter.product() would panic on overflow. So instead iter.product<OverflowInteger<T>>() can be used (for integer type T) which will perform the product computation and return an OverflowInteger without ever panicking.

template <class U>
static auto try_from(U u) -> Result<OverflowInteger<I>, TryFromIntError>
requires
sus::construct::TryFrom<I, U>

Satisfies sus::construct::TryFrom<OverflowInteger<I>, U> if the inner integer type I satisfies sus::construct::TryFrom<I, U>.

Methods

auto as_value() const& -> I
auto as_value_mut() & -> I&
auto is_overflow() const -> bool
auto is_valid() const -> bool
auto to_option() const -> Option<I>

Converts the OverflowInteger into an Option that contains the integer value. If overflow has occured, returns None.

auto unwrap() && -> I

Operators

auto operator%=(I rhs) -> void
auto operator%=(OverflowInteger<I> rhs) -> void
auto operator*=(I rhs) -> void
auto operator*=(OverflowInteger<I> rhs) -> void
auto operator+=(I rhs) -> void
auto operator+=(OverflowInteger<I> rhs) -> void
auto operator-=(I rhs) -> void
auto operator-=(OverflowInteger<I> rhs) -> void
auto operator/=(I rhs) -> void
auto operator/=(OverflowInteger<I> rhs) -> void