CheckedAdd

Add two integers, checking for overflow.

Synopsis

Declared in <util/overflow.h>

template<class T>
[[nodiscard]]
std::optional<T>
CheckedAdd(
    T const i,
    T const j) noexcept;

Return Value

The sum i + j, or std::nullopt if the addition would overflow T.

NOTE

The return value should not be discarded.

Parameters

NameDescription
iThe first addend.
jThe second addend.