Skip to content

Commit

Permalink
Simplify overflowing_cast
Browse files Browse the repository at this point in the history
Use the new C++20 std::in_range(), which seems to generate better code with GCC (and exactly the same with Clang)
  • Loading branch information
tcbrindle committed Aug 22, 2024
1 parent c975597 commit 503dc4c
Showing 1 changed file with 1 addition and 10 deletions.
11 changes: 1 addition & 10 deletions include/flux/core/numeric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,7 @@ struct overflowing_cast_fn {
if constexpr (requires { To{from}; }) {
return {To{from}, false}; // not a narrowing conversion
} else {
To to = static_cast<To>(from);
if (static_cast<From>(to) != from) {
return {to, true};
}
if constexpr (std::is_signed_v<From> != std::is_signed_v<To>) {
if ((to < To{}) != (from < From{})) {
return {to, true};
}
}
return {to, false};
return {static_cast<To>(from), !std::in_range<To>(from)};
}
}
};
Expand Down

0 comments on commit 503dc4c

Please sign in to comment.