| Index: base/numerics/safe_math_impl.h
|
| diff --git a/base/numerics/safe_math_impl.h b/base/numerics/safe_math_impl.h
|
| index 1bb5c5b83f2ee1f8ff39c7cc92976fb030dada6d..d6977f4080f73db95bac029d7a6a6c9fd87119d2 100644
|
| --- a/base/numerics/safe_math_impl.h
|
| +++ b/base/numerics/safe_math_impl.h
|
| @@ -10,6 +10,7 @@
|
| #include <cmath>
|
| #include <cstdlib>
|
| #include <limits>
|
| +#include <type_traits>
|
|
|
| #include "base/numerics/safe_conversions.h"
|
| #include "base/template_util.h"
|
| @@ -63,21 +64,21 @@ struct IntegerForSizeAndSign<8, false> {
|
|
|
| template <typename Integer>
|
| struct UnsignedIntegerForSize {
|
| - typedef typename enable_if<
|
| + typedef typename std::enable_if<
|
| std::numeric_limits<Integer>::is_integer,
|
| typename IntegerForSizeAndSign<sizeof(Integer), false>::type>::type type;
|
| };
|
|
|
| template <typename Integer>
|
| struct SignedIntegerForSize {
|
| - typedef typename enable_if<
|
| + typedef typename std::enable_if<
|
| std::numeric_limits<Integer>::is_integer,
|
| typename IntegerForSizeAndSign<sizeof(Integer), true>::type>::type type;
|
| };
|
|
|
| template <typename Integer>
|
| struct TwiceWiderInteger {
|
| - typedef typename enable_if<
|
| + typedef typename std::enable_if<
|
| std::numeric_limits<Integer>::is_integer,
|
| typename IntegerForSizeAndSign<
|
| sizeof(Integer) * 2,
|
| @@ -86,8 +87,9 @@ struct TwiceWiderInteger {
|
|
|
| template <typename Integer>
|
| struct PositionOfSignBit {
|
| - static const typename enable_if<std::numeric_limits<Integer>::is_integer,
|
| - size_t>::type value = 8 * sizeof(Integer) - 1;
|
| + static const typename std::enable_if<std::numeric_limits<Integer>::is_integer,
|
| + size_t>::type value =
|
| + 8 * sizeof(Integer) - 1;
|
| };
|
|
|
| // This is used for UnsignedAbs, where we need to support floating-point
|
| @@ -129,7 +131,7 @@ T BinaryComplement(T x) {
|
| // way to coalesce things into the CheckedNumericState specializations below.
|
|
|
| template <typename T>
|
| -typename enable_if<std::numeric_limits<T>::is_integer, T>::type
|
| +typename std::enable_if<std::numeric_limits<T>::is_integer, T>::type
|
| CheckedAdd(T x, T y, RangeConstraint* validity) {
|
| // Since the value of x+y is undefined if we have a signed type, we compute
|
| // it using the unsigned type of the same size.
|
| @@ -152,7 +154,7 @@ CheckedAdd(T x, T y, RangeConstraint* validity) {
|
| }
|
|
|
| template <typename T>
|
| -typename enable_if<std::numeric_limits<T>::is_integer, T>::type
|
| +typename std::enable_if<std::numeric_limits<T>::is_integer, T>::type
|
| CheckedSub(T x, T y, RangeConstraint* validity) {
|
| // Since the value of x+y is undefined if we have a signed type, we compute
|
| // it using the unsigned type of the same size.
|
| @@ -179,9 +181,9 @@ CheckedSub(T x, T y, RangeConstraint* validity) {
|
| // slow case we need to manually check that the result won't be truncated by
|
| // checking with division against the appropriate bound.
|
| template <typename T>
|
| -typename enable_if<
|
| - std::numeric_limits<T>::is_integer && sizeof(T) * 2 <= sizeof(uintmax_t),
|
| - T>::type
|
| +typename std::enable_if<std::numeric_limits<T>::is_integer &&
|
| + sizeof(T) * 2 <= sizeof(uintmax_t),
|
| + T>::type
|
| CheckedMul(T x, T y, RangeConstraint* validity) {
|
| typedef typename TwiceWiderInteger<T>::type IntermediateType;
|
| IntermediateType tmp =
|
| @@ -191,9 +193,10 @@ CheckedMul(T x, T y, RangeConstraint* validity) {
|
| }
|
|
|
| template <typename T>
|
| -typename enable_if<std::numeric_limits<T>::is_integer&& std::numeric_limits<
|
| - T>::is_signed&&(sizeof(T) * 2 > sizeof(uintmax_t)),
|
| - T>::type
|
| +typename std::enable_if<std::numeric_limits<T>::is_integer &&
|
| + std::numeric_limits<T>::is_signed &&
|
| + (sizeof(T) * 2 > sizeof(uintmax_t)),
|
| + T>::type
|
| CheckedMul(T x, T y, RangeConstraint* validity) {
|
| // If either side is zero then the result will be zero.
|
| if (!x || !y) {
|
| @@ -220,10 +223,10 @@ CheckedMul(T x, T y, RangeConstraint* validity) {
|
| }
|
|
|
| template <typename T>
|
| -typename enable_if<std::numeric_limits<T>::is_integer &&
|
| - !std::numeric_limits<T>::is_signed &&
|
| - (sizeof(T) * 2 > sizeof(uintmax_t)),
|
| - T>::type
|
| +typename std::enable_if<std::numeric_limits<T>::is_integer &&
|
| + !std::numeric_limits<T>::is_signed &&
|
| + (sizeof(T) * 2 > sizeof(uintmax_t)),
|
| + T>::type
|
| CheckedMul(T x, T y, RangeConstraint* validity) {
|
| *validity = (y == 0 || x <= std::numeric_limits<T>::max() / y)
|
| ? RANGE_VALID
|
| @@ -233,11 +236,11 @@ CheckedMul(T x, T y, RangeConstraint* validity) {
|
|
|
| // Division just requires a check for an invalid negation on signed min/-1.
|
| template <typename T>
|
| -T CheckedDiv(
|
| - T x,
|
| - T y,
|
| - RangeConstraint* validity,
|
| - typename enable_if<std::numeric_limits<T>::is_integer, int>::type = 0) {
|
| +T CheckedDiv(T x,
|
| + T y,
|
| + RangeConstraint* validity,
|
| + typename std::enable_if<std::numeric_limits<T>::is_integer,
|
| + int>::type = 0) {
|
| if (std::numeric_limits<T>::is_signed && x == std::numeric_limits<T>::min() &&
|
| y == static_cast<T>(-1)) {
|
| *validity = RANGE_OVERFLOW;
|
| @@ -249,27 +252,27 @@ T CheckedDiv(
|
| }
|
|
|
| template <typename T>
|
| -typename enable_if<
|
| - std::numeric_limits<T>::is_integer&& std::numeric_limits<T>::is_signed,
|
| - T>::type
|
| +typename std::enable_if<std::numeric_limits<T>::is_integer &&
|
| + std::numeric_limits<T>::is_signed,
|
| + T>::type
|
| CheckedMod(T x, T y, RangeConstraint* validity) {
|
| *validity = y > 0 ? RANGE_VALID : RANGE_INVALID;
|
| return x % y;
|
| }
|
|
|
| template <typename T>
|
| -typename enable_if<
|
| - std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
|
| - T>::type
|
| +typename std::enable_if<std::numeric_limits<T>::is_integer &&
|
| + !std::numeric_limits<T>::is_signed,
|
| + T>::type
|
| CheckedMod(T x, T y, RangeConstraint* validity) {
|
| *validity = RANGE_VALID;
|
| return x % y;
|
| }
|
|
|
| template <typename T>
|
| -typename enable_if<
|
| - std::numeric_limits<T>::is_integer&& std::numeric_limits<T>::is_signed,
|
| - T>::type
|
| +typename std::enable_if<std::numeric_limits<T>::is_integer &&
|
| + std::numeric_limits<T>::is_signed,
|
| + T>::type
|
| CheckedNeg(T value, RangeConstraint* validity) {
|
| *validity =
|
| value != std::numeric_limits<T>::min() ? RANGE_VALID : RANGE_OVERFLOW;
|
| @@ -278,9 +281,9 @@ CheckedNeg(T value, RangeConstraint* validity) {
|
| }
|
|
|
| template <typename T>
|
| -typename enable_if<
|
| - std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
|
| - T>::type
|
| +typename std::enable_if<std::numeric_limits<T>::is_integer &&
|
| + !std::numeric_limits<T>::is_signed,
|
| + T>::type
|
| CheckedNeg(T value, RangeConstraint* validity) {
|
| // The only legal unsigned negation is zero.
|
| *validity = value ? RANGE_UNDERFLOW : RANGE_VALID;
|
| @@ -289,9 +292,9 @@ CheckedNeg(T value, RangeConstraint* validity) {
|
| }
|
|
|
| template <typename T>
|
| -typename enable_if<
|
| - std::numeric_limits<T>::is_integer&& std::numeric_limits<T>::is_signed,
|
| - T>::type
|
| +typename std::enable_if<std::numeric_limits<T>::is_integer &&
|
| + std::numeric_limits<T>::is_signed,
|
| + T>::type
|
| CheckedAbs(T value, RangeConstraint* validity) {
|
| *validity =
|
| value != std::numeric_limits<T>::min() ? RANGE_VALID : RANGE_OVERFLOW;
|
| @@ -299,9 +302,9 @@ CheckedAbs(T value, RangeConstraint* validity) {
|
| }
|
|
|
| template <typename T>
|
| -typename enable_if<
|
| - std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
|
| - T>::type
|
| +typename std::enable_if<std::numeric_limits<T>::is_integer &&
|
| + !std::numeric_limits<T>::is_signed,
|
| + T>::type
|
| CheckedAbs(T value, RangeConstraint* validity) {
|
| // T is unsigned, so |value| must already be positive.
|
| *validity = RANGE_VALID;
|
| @@ -309,9 +312,9 @@ CheckedAbs(T value, RangeConstraint* validity) {
|
| }
|
|
|
| template <typename T>
|
| -typename enable_if<std::numeric_limits<T>::is_integer &&
|
| - std::numeric_limits<T>::is_signed,
|
| - typename UnsignedIntegerForSize<T>::type>::type
|
| +typename std::enable_if<std::numeric_limits<T>::is_integer &&
|
| + std::numeric_limits<T>::is_signed,
|
| + typename UnsignedIntegerForSize<T>::type>::type
|
| CheckedUnsignedAbs(T value) {
|
| typedef typename UnsignedIntegerForSize<T>::type UnsignedT;
|
| return value == std::numeric_limits<T>::min()
|
| @@ -320,9 +323,9 @@ CheckedUnsignedAbs(T value) {
|
| }
|
|
|
| template <typename T>
|
| -typename enable_if<std::numeric_limits<T>::is_integer &&
|
| - !std::numeric_limits<T>::is_signed,
|
| - T>::type
|
| +typename std::enable_if<std::numeric_limits<T>::is_integer &&
|
| + !std::numeric_limits<T>::is_signed,
|
| + T>::type
|
| CheckedUnsignedAbs(T value) {
|
| // T is unsigned, so |value| must already be positive.
|
| return value;
|
| @@ -330,12 +333,12 @@ CheckedUnsignedAbs(T value) {
|
|
|
| // These are the floating point stubs that the compiler needs to see. Only the
|
| // negation operation is ever called.
|
| -#define BASE_FLOAT_ARITHMETIC_STUBS(NAME) \
|
| - template <typename T> \
|
| - typename enable_if<std::numeric_limits<T>::is_iec559, T>::type \
|
| - Checked##NAME(T, T, RangeConstraint*) { \
|
| - NOTREACHED(); \
|
| - return 0; \
|
| +#define BASE_FLOAT_ARITHMETIC_STUBS(NAME) \
|
| + template <typename T> \
|
| + typename std::enable_if<std::numeric_limits<T>::is_iec559, T>::type \
|
| + Checked##NAME(T, T, RangeConstraint*) { \
|
| + NOTREACHED(); \
|
| + return 0; \
|
| }
|
|
|
| BASE_FLOAT_ARITHMETIC_STUBS(Add)
|
| @@ -347,14 +350,14 @@ BASE_FLOAT_ARITHMETIC_STUBS(Mod)
|
| #undef BASE_FLOAT_ARITHMETIC_STUBS
|
|
|
| template <typename T>
|
| -typename enable_if<std::numeric_limits<T>::is_iec559, T>::type CheckedNeg(
|
| +typename std::enable_if<std::numeric_limits<T>::is_iec559, T>::type CheckedNeg(
|
| T value,
|
| RangeConstraint*) {
|
| return -value;
|
| }
|
|
|
| template <typename T>
|
| -typename enable_if<std::numeric_limits<T>::is_iec559, T>::type CheckedAbs(
|
| +typename std::enable_if<std::numeric_limits<T>::is_iec559, T>::type CheckedAbs(
|
| T value,
|
| RangeConstraint*) {
|
| return std::abs(value);
|
| @@ -414,8 +417,8 @@ class CheckedNumericState<T, NUMERIC_INTEGER> {
|
| template <typename Src>
|
| explicit CheckedNumericState(
|
| Src value,
|
| - typename enable_if<std::numeric_limits<Src>::is_specialized, int>::type =
|
| - 0)
|
| + typename std::enable_if<std::numeric_limits<Src>::is_specialized,
|
| + int>::type = 0)
|
| : value_(static_cast<T>(value)),
|
| validity_(DstRangeRelationToSrcRange<T>(value)) {}
|
|
|
| @@ -439,7 +442,8 @@ class CheckedNumericState<T, NUMERIC_FLOATING> {
|
| CheckedNumericState(
|
| Src value,
|
| RangeConstraint validity,
|
| - typename enable_if<std::numeric_limits<Src>::is_integer, int>::type = 0) {
|
| + typename std::enable_if<std::numeric_limits<Src>::is_integer, int>::type =
|
| + 0) {
|
| switch (DstRangeRelationToSrcRange<T>(value)) {
|
| case RANGE_VALID:
|
| value_ = static_cast<T>(value);
|
| @@ -465,8 +469,8 @@ class CheckedNumericState<T, NUMERIC_FLOATING> {
|
| template <typename Src>
|
| explicit CheckedNumericState(
|
| Src value,
|
| - typename enable_if<std::numeric_limits<Src>::is_specialized, int>::type =
|
| - 0)
|
| + typename std::enable_if<std::numeric_limits<Src>::is_specialized,
|
| + int>::type = 0)
|
| : value_(static_cast<T>(value)) {}
|
|
|
| // Copy constructor.
|
|
|