mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-07-28 00:59:03 +02:00
util: Require integers for SaturatingAdd() and AdditionOverflow()
Previously we could fall back to using an unspecialized implementation of std::numeric_limits<T> which would compile as long as the numeric operators existed, but would return 0 for min() & max().
This commit is contained in:
@@ -13,10 +13,9 @@
|
||||
#include <optional>
|
||||
#include <type_traits>
|
||||
|
||||
template <class T>
|
||||
template <std::integral T>
|
||||
[[nodiscard]] bool AdditionOverflow(const T i, const T j) noexcept
|
||||
{
|
||||
static_assert(std::is_integral_v<T>, "Integral required.");
|
||||
if constexpr (std::numeric_limits<T>::is_signed) {
|
||||
return (i > 0 && j > std::numeric_limits<T>::max() - i) ||
|
||||
(i < 0 && j < std::numeric_limits<T>::min() - i);
|
||||
@@ -41,7 +40,7 @@ template <std::unsigned_integral T, std::unsigned_integral U>
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
template <std::integral T>
|
||||
[[nodiscard]] T SaturatingAdd(const T i, const T j) noexcept
|
||||
{
|
||||
if constexpr (std::numeric_limits<T>::is_signed) {
|
||||
|
||||
Reference in New Issue
Block a user