diff --git a/include/gsl/gsl_util b/include/gsl/gsl_util index d4f5563..89fb2ee 100644 --- a/include/gsl/gsl_util +++ b/include/gsl/gsl_util @@ -90,15 +90,6 @@ struct narrowing_error : public std::exception { }; -namespace details -{ - template - struct is_same_signedness - : public std::integral_constant::value == std::is_signed::value> - { - }; -} // namespace details - // narrow() : a checked version of narrow_cast() that throws if the cast changed the value template GSL_SUPPRESS(type.1) // NO-FORMAT: attribute @@ -106,10 +97,17 @@ GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recogn constexpr T narrow(U u) noexcept(false) { - T t = narrow_cast(u); - if (static_cast(t) != u) throw narrowing_error{}; - if (!details::is_same_signedness::value && ((t < T{}) != (u < U{}))) + constexpr const bool is_different_signedness = (std::is_signed::value != std::is_signed::value); + + const T t = narrow_cast(u); + + if (static_cast(t) != u + || (is_different_signedness + && ((t < T{}) != (u < U{})))) + { throw narrowing_error{}; + } + return t; }