23 requires std::is_arithmetic_v<T>
24 [[deprecated(
"Use MathUtil::SafelyAddWithCap instead")]]
27 using ForMath = std::conditional_t<std::is_integral_v<T> &&
sizeof(T) <
sizeof(
int), int, T>;
29 const auto a =
static_cast<ForMath
>(base);
30 const auto b =
static_cast<ForMath
>(increment);
31 const auto max =
static_cast<ForMath
>(std::numeric_limits<T>::max());
33 return static_cast<T
>(a + b > max ? max : a + b);
44 requires std::is_arithmetic_v<T>
45 static T
SafelyAddWithCap(T base, T increment, T cap = std::numeric_limits<T>::max()) {
47 using ForMath = std::conditional_t<std::is_integral_v<T> &&
sizeof(T) <
sizeof(
int), int, T>;
49 const auto a =
static_cast<ForMath
>(base);
50 const auto b =
static_cast<ForMath
>(increment);
51 const auto max = std::min(
static_cast<ForMath
>(cap),
static_cast<ForMath
>(std::numeric_limits<T>::max()));
53 return static_cast<T
>(a + b > max ? max : a + b);
static T SafelyAddWithCap(T base, T increment, T cap=std::numeric_limits< T >::max())
Add a value to another one and cap the result.
Definition st-math.h:45
static T SafelyAdd(T base, T increment)
Template function to add to a value without ever risking overflowing the maximum value that type can ...
Definition st-math.h:25