| 37 | std::numeric_limits<From>::is_signed == |
| 38 | std::numeric_limits<To>::is_signed)> |
| 39 | FMT_CONSTEXPR To lossless_integral_conversion(const From from, int& ec) { |
| 40 | ec = 0; |
| 41 | using F = std::numeric_limits<From>; |
| 42 | using T = std::numeric_limits<To>; |
| 43 | static_assert(F::is_integer, "From must be integral"); |
| 44 | static_assert(T::is_integer, "To must be integral"); |
| 45 | |
| 46 | // A and B are both signed, or both unsigned. |
| 47 | if (F::digits <= T::digits) { |
| 48 | // From fits in To without any problem. |
| 49 | } else { |
| 50 | // From does not always fit in To, resort to a dynamic check. |
| 51 | if (from < T::min() || from > T::max()) { |
| 52 | // outside range. |
| 53 | ec = 1; |
| 54 | return {}; |
| 55 | } |
| 56 | } |
| 57 | return static_cast<To>(from); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * converts From to To, without loss. If the dynamic value of from |
nothing calls this directly
no test coverage detected