diff options
| author | Robert Andrzejuk <robert-andrzejuk@users.noreply.github.com> | 2020-04-29 20:11:44 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-29 20:11:44 +0200 |
| commit | 1e5f44d3eaf2f87df3bd0fafb61ab1bd0fadcce2 (patch) | |
| tree | bac7f7f705d065373cb4547e54b335452b90efb9 | |
| parent | 1999b48a519196711f0d03af3b7eedd49fcc6db3 (diff) | |
| download | platform_external_Microsoft-GSL-1e5f44d3eaf2f87df3bd0fafb61ab1bd0fadcce2.tar.gz platform_external_Microsoft-GSL-1e5f44d3eaf2f87df3bd0fafb61ab1bd0fadcce2.tar.bz2 platform_external_Microsoft-GSL-1e5f44d3eaf2f87df3bd0fafb61ab1bd0fadcce2.zip | |
Refactor `narrow` - simplify & move `is_same_signedness` into function, remove uneeded `detail` namespace. Merge 2 `if`'s with a `||`.
| -rw-r--r-- | include/gsl/gsl_util | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/include/gsl/gsl_util b/include/gsl/gsl_util index bc65923..cdb94ac 100644 --- a/include/gsl/gsl_util +++ b/include/gsl/gsl_util @@ -100,15 +100,6 @@ struct narrowing_error : public std::exception { }; -namespace details -{ - template <class T, class U> - struct is_same_signedness - : public std::integral_constant<bool, std::is_signed<T>::value == std::is_signed<U>::value> - { - }; -} // namespace details - // narrow() : a checked version of narrow_cast() that throws if the cast changed the value template <class T, class U> GSL_SUPPRESS(type.1) // NO-FORMAT: attribute @@ -118,10 +109,17 @@ constexpr #endif T narrow(U u) noexcept(false) { - T t = narrow_cast<T>(u); - if (static_cast<U>(t) != u) throw narrowing_error{}; - if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{}))) + constexpr const bool is_same_signedness = std::is_signed<T>::value == std::is_signed<U>::value; + + const T t = narrow_cast<T>(u); + + if (static_cast<U>(t) != u + || (!is_same_signedness + && ((t < T{}) != (u < U{})))) + { throw narrowing_error{}; + } + return t; } |
