42 static_assert(std::is_integral_v<T>,
"T must be an integral type");
57 return value_.value();
65 return value_.error();
71 tl::expected<T, SafeIntError>
expected()
const {
86 value_ =
add(*value_, b);
114 value_ =
sub(*value_, b);
142 value_ =
mul(*value_, b);
170 value_ =
div(*value_, b);
195 static tl::expected<T, SafeIntError>
add(T a, U b) {
196 if (b > 0 && a > std::numeric_limits<T>::max() - b) {
199 if (b < 0 && a < std::numeric_limits<T>::min() - b) {
213 static tl::expected<T, SafeIntError>
sub(T a, U b) {
214 if (b < 0 && a > std::numeric_limits<T>::max() + b) {
217 if (b > 0 && a < std::numeric_limits<T>::min() + b) {
231 static tl::expected<T, SafeIntError>
mul(T a, U b) {
233 if (a == 0 || b == 0) {
240 if (a > std::numeric_limits<T>::max() / b) {
244 if (b < std::numeric_limits<T>::min() / a) {
250 if (a < std::numeric_limits<T>::min() / b) {
254 if (a < std::numeric_limits<T>::max() / b) {
272 static tl::expected<T, SafeIntError>
div(T a, U b) {
279 if constexpr (std::is_signed_v<T>) {
280 if (a == std::numeric_limits<T>::min() && b == -1) {
289 tl::expected<T, SafeIntError> value_ {};
A safe integer type that checks for overflow and underflow when performing arithmetic operations.
SafeInt(T value)
Constructs a safe_int from a value.
static tl::expected< T, SafeIntError > div(T a, U b)
Divides two values, returning an error if the operation would overflow or underflow.
SafeInt & operator+=(U b)
Adds a value to the safe_int, returning an error if the operation would overflow or underflow.
tl::expected< T, SafeIntError > expected() const
SafeInt operator+(U b) const
Adds a value to the safe_int, returning a new safe_int with the result.
SafeInt operator-(U b) const
Subtracts a value from the safe_int, returning a new safe_int with the result.
SafeInt operator*(U b) const
Multiplies the safe_int by a scalar, returning a new safe_int with the result.
SafeInt & operator/=(U b)
Divides the safe_int by a scalar, returning an error if the operation would overflow or underflow.
SafeInt & operator-=(U b)
Subtracts a value from the safe_int, returning an error if the operation would overflow or underflow.
SafeInt & operator*=(U b)
Multiplies the safe_int by a scalar, returning an error if the operation would overflow or underflow.
SafeIntError error() const
static tl::expected< T, SafeIntError > add(T a, U b)
Adds two values, returning an error if the operation would overflow or underflow.
SafeInt operator/(U b) const
Divides the safe_int by a scalar, returning a new safe_int with the result.
static tl::expected< T, SafeIntError > mul(T a, U b)
Multiplies two values, returning an error if the operation would overflow or underflow.
static tl::expected< T, SafeIntError > sub(T a, U b)
Subtracts two values, returning an error if the operation would overflow or underflow.
SafeInt< uint8_t > SafeUint8
SafeInt< int64_t > SafeInt64
SafeInt< uint32_t > SafeUint32
SafeInt< int32_t > SafeInt32
SafeInt< int16_t > SafeInt16
SafeInt< uint64_t > SafeUint64
SafeInt< int8_t > SafeInt8
SafeInt< uint16_t > SafeUint16