mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
narrow: Check for changed sign only if types have different signess.
This commit is contained in:
parent
6a4f2512b7
commit
092a8e53e4
@ -22,6 +22,7 @@
|
|||||||
#include "gsl_assert.h" // Ensures/Expects
|
#include "gsl_assert.h" // Ensures/Expects
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <type_traits>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
@ -90,10 +91,24 @@ inline constexpr T narrow_cast(U u) noexcept
|
|||||||
|
|
||||||
struct narrowing_error : public std::exception {};
|
struct narrowing_error : public std::exception {};
|
||||||
|
|
||||||
|
template<class T, class U>
|
||||||
|
struct is_same_signess
|
||||||
|
{
|
||||||
|
static const bool value =
|
||||||
|
std::is_signed<T>::value == std::is_signed<U>::value;
|
||||||
|
};
|
||||||
|
|
||||||
// narrow() : a checked version of narrow_cast() that throws if the cast changed the value
|
// narrow() : a checked version of narrow_cast() that throws if the cast changed the value
|
||||||
template<class T, class U>
|
template<class T, class U>
|
||||||
inline T narrow(U u)
|
inline T narrow(U u)
|
||||||
{ T t = narrow_cast<T>(u); if (static_cast<U>(t) != u || (t < T{}) != (u < U{})) throw narrowing_error(); return t; }
|
{
|
||||||
|
T t = narrow_cast<T>(u);
|
||||||
|
if (static_cast<U>(t) != u)
|
||||||
|
throw narrowing_error();
|
||||||
|
if (!is_same_signess<T, U>::value && ((t < T{}) != (u < U{})))
|
||||||
|
throw narrowing_error();
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// at() - Bounds-checked way of accessing static arrays, std::array, std::vector
|
// at() - Bounds-checked way of accessing static arrays, std::array, std::vector
|
||||||
|
Loading…
Reference in New Issue
Block a user