49 static_assert(std::is_integral_v<From>,
"From has to be an integral type.");
50 static_assert(std::is_integral_v<To>,
"To has to be an integral type.");
54 std::is_signed_v<From> || std::numeric_limits<From>::min() == 0,
55 "The type From has to include the number 0."
58 std::is_signed_v<To> || std::numeric_limits<To>::min() == 0,
59 "The type To has to include the number 0."
63 if constexpr (std::is_signed_v<From> && std::is_signed_v<To>) {
65 std::numeric_limits<From>::digits <= std::numeric_limits<intmax_t>::digits,
66 "From has more bits than intmax_t."
69 std::numeric_limits<To>::digits <= std::numeric_limits<intmax_t>::digits,
70 "To has more bits than intmax_t."
74 std::numeric_limits<From>::digits <= std::numeric_limits<uintmax_t>::digits,
75 "From has more bits than uintmax_t."
78 std::numeric_limits<To>::digits <= std::numeric_limits<uintmax_t>::digits,
79 "To has more bits than uintmax_t."
84 if constexpr (std::is_unsigned_v<From> && std::is_unsigned_v<To>) {
85 return static_cast<uintmax_t>(value) <=
static_cast<uintmax_t>(std::numeric_limits<To>::max());
86 }
else if constexpr (std::is_signed_v<From> && std::is_signed_v<To>) {
87 return static_cast<intmax_t>(value) >=
static_cast<intmax_t>(std::numeric_limits<To>::min())
88 &&
static_cast<intmax_t>(value) <=
static_cast<intmax_t>(std::numeric_limits<To>::max());
89 }
else if constexpr (std::is_signed_v<From> && std::is_unsigned_v<To>) {
93 return static_cast<uintmax_t>(value) <=
static_cast<uintmax_t>(std::numeric_limits<To>::max());
95 }
else if constexpr (std::is_unsigned_v<From> && std::is_signed_v<To>) {
96 return static_cast<uintmax_t>(value) <=
static_cast<uintmax_t>(std::numeric_limits<To>::max());
constexpr To throwing_cast(From value)
Casts an integer value to the integer type To. If the value is outside To's range,...
Definition checking_casts.hpp:136
constexpr To asserting_cast(From value) KAMPING_NOEXCEPT
Casts an integer value to the integer type To. If the value is outside To's range,...
Definition checking_casts.hpp:116
constexpr bool in_range(From value) noexcept
Checks if an integer value can be safely casted into an integer type To, that is, it lies in the rang...
Definition checking_casts.hpp:48