From 611a321a6e641846497f75375f9cbe7825ff3f23 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 21 Oct 2019 10:56:09 -0400 Subject: Googletest export Remove bool_constant in favor of std::integral_constant; The one non-trivial use of bool_constant has been changed to have significantly fewer template specializations. PiperOrigin-RevId: 275842490 --- googlemock/include/gmock/gmock-matchers.h | 2 +- .../include/gmock/internal/gmock-internal-utils.h | 92 +++++++--------------- googletest/include/gtest/internal/gtest-internal.h | 3 +- googletest/include/gtest/internal/gtest-port.h | 3 - 4 files changed, 29 insertions(+), 71 deletions(-) diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index be446aa..bb047da 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -142,7 +142,7 @@ class MatcherCastImpl { template static Matcher CastImpl(const M& polymorphic_matcher_or_value, std::true_type /* convertible_to_matcher */, - bool_constant) { + std::integral_constant) { // M is implicitly convertible to Matcher, which means that either // M is a polymorphic matcher or Matcher has an implicit constructor // from M. In both cases using the implicit conversion will produce a diff --git a/googlemock/include/gmock/internal/gmock-internal-utils.h b/googlemock/include/gmock/internal/gmock-internal-utils.h index 584afa9..5fd169e 100644 --- a/googlemock/include/gmock/internal/gmock-internal-utils.h +++ b/googlemock/include/gmock/internal/gmock-internal-utils.h @@ -157,9 +157,6 @@ GMOCK_DECLARE_KIND_(long double, kFloatingPoint); static_cast< ::testing::internal::TypeKind>( \ ::testing::internal::KindOf::value) -// Evaluates to true if and only if integer type T is signed. -#define GMOCK_IS_SIGNED_(T) (static_cast(-1) < 0) - // LosslessArithmeticConvertibleImpl::value // is true if and only if arithmetic type From can be losslessly converted to // arithmetic type To. @@ -170,65 +167,30 @@ GMOCK_DECLARE_KIND_(long double, kFloatingPoint); // From, and kToKind is the kind of To; the value is // implementation-defined when the above pre-condition is violated. template -struct LosslessArithmeticConvertibleImpl : public std::false_type {}; - -// Converting bool to bool is lossless. -template <> -struct LosslessArithmeticConvertibleImpl - : public std::true_type {}; - -// Converting bool to any integer type is lossless. -template -struct LosslessArithmeticConvertibleImpl - : public std::true_type {}; - -// Converting bool to any floating-point type is lossless. -template -struct LosslessArithmeticConvertibleImpl - : public std::true_type {}; - -// Converting an integer to bool is lossy. -template -struct LosslessArithmeticConvertibleImpl - : public std::false_type {}; - -// Converting an integer to another non-bool integer is lossless -// if and only if the target type's range encloses the source type's range. -template -struct LosslessArithmeticConvertibleImpl - : public bool_constant< - // When converting from a smaller size to a larger size, we are - // fine as long as we are not converting from signed to unsigned. - ((sizeof(From) < sizeof(To)) && - (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) || - // When converting between the same size, the signedness must match. - ((sizeof(From) == sizeof(To)) && - (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {}; // NOLINT - -#undef GMOCK_IS_SIGNED_ - -// Converting an integer to a floating-point type may be lossy, since -// the format of a floating-point number is implementation-defined. -template -struct LosslessArithmeticConvertibleImpl - : public std::false_type {}; - -// Converting a floating-point to bool is lossy. -template -struct LosslessArithmeticConvertibleImpl - : public std::false_type {}; - -// Converting a floating-point to an integer is lossy. -template -struct LosslessArithmeticConvertibleImpl - : public std::false_type {}; - -// Converting a floating-point to another floating-point is lossless -// if and only if the target type is at least as big as the source type. -template -struct LosslessArithmeticConvertibleImpl< - kFloatingPoint, From, kFloatingPoint, To> - : public bool_constant {}; // NOLINT +using LosslessArithmeticConvertibleImpl = std::integral_constant< + bool, + // clang-format off + // Converting from bool is always lossless + (kFromKind == kBool) ? true + // Converting between any other type kinds will be lossy if the type + // kinds are not the same. + : (kFromKind != kToKind) ? false + : (kFromKind == kInteger && + // Converting between integers of different widths is allowed so long + // as the conversion does not go from signed to unsigned. + (((sizeof(From) < sizeof(To)) && + !(std::is_signed::value && !std::is_signed::value)) || + // Converting between integers of the same width only requires the + // two types to have the same signedness. + ((sizeof(From) == sizeof(To)) && + (std::is_signed::value == std::is_signed::value))) + ) ? true + // Floating point conversions are lossless if and only if `To` is at least + // as wide as `From`. + : (kFromKind == kFloatingPoint && (sizeof(From) <= sizeof(To))) ? true + : false + // clang-format on + >; // LosslessArithmeticConvertible::value is true if and only if // arithmetic type From can be losslessly converted to arithmetic type To. @@ -238,9 +200,9 @@ struct LosslessArithmeticConvertibleImpl< // reference) built-in arithmetic types; the value is // implementation-defined when the above pre-condition is violated. template -struct LosslessArithmeticConvertible - : public LosslessArithmeticConvertibleImpl< - GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {}; // NOLINT +using LosslessArithmeticConvertible = + LosslessArithmeticConvertibleImpl; // This interface knows how to report a Google Mock failure (either // non-fatal or fatal). diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index ebfe3c9..9524223 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -855,8 +855,7 @@ class GTEST_API_ Random { // true if and only if T is type proto2::Message or a subclass of it. template struct IsAProtocolMessage - : public bool_constant< - std::is_convertible::value> {}; + : public std::is_convertible {}; // When the compiler sees expression IsContainerTest(0), if C is an // STL-style container class, the first overload of IsContainerTest diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index c4b1aab..dc67902 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -1875,9 +1875,6 @@ class GTEST_API_ ThreadLocal { // we cannot detect it. GTEST_API_ size_t GetThreadCount(); -template -using bool_constant = std::integral_constant; - #if GTEST_OS_WINDOWS # define GTEST_PATH_SEP_ "\\" # define GTEST_HAS_ALT_PATH_SEP_ 1 -- cgit v0.12