mirror of
https://github.com/microsoft/GSL.git
synced 2025-05-13 09:25:19 -04:00
Changed implementation of gsl::narrow to throw gsl::narrowing_error
Implementation now behaves as described in the C++ Core Guidlines
This commit is contained in:
@ -96,6 +96,10 @@ constexpr T narrow_cast(U&& u) noexcept
|
||||
return static_cast<T>(std::forward<U>(u));
|
||||
}
|
||||
|
||||
struct narrowing_error : public std::exception
|
||||
{
|
||||
};
|
||||
|
||||
namespace details
|
||||
{
|
||||
template <class T, class U>
|
||||
@ -115,9 +119,9 @@ constexpr
|
||||
T narrow(U u) noexcept(false)
|
||||
{
|
||||
T t = narrow_cast<T>(u);
|
||||
if (static_cast<U>(t) != u) gsl::details::terminate();
|
||||
if (static_cast<U>(t) != u) throw narrowing_error{};
|
||||
if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{})))
|
||||
gsl::details::terminate();
|
||||
throw narrowing_error{};
|
||||
return t;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user