SaturatingAdd

Add two integers, saturating instead of overflowing.

Synopsis

Declared in <util/overflow.h>

template<std::integral T>
[[nodiscard]]
T
SaturatingAdd(
    T const i,
    T const j) noexcept;

Return Value

The sum i + j, clamped to the lowest or highest representable value of T when the addition would overflow.

NOTE

The return value should not be discarded.

Parameters

NameDescription
iThe first addend.
jThe second addend.