From f2fb48c3b3d79a75a88a99fba6576b25d42ec528 Mon Sep 17 00:00:00 2001 From: kuzkry Date: Mon, 16 Sep 2019 01:46:55 -0400 Subject: Googletest export Merge 3bdefdb473d304803d2a38e2a2cd5cdc1827c3bd into fb49e6c164490a227bbb7cf5223b846c836a0305 Closes #2407 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/googletest/pull/2407 from kuzkry:StaticAssertTypeEq 3bdefdb473d304803d2a38e2a2cd5cdc1827c3bd PiperOrigin-RevId: 269255328 --- googlemock/include/gmock/gmock-matchers.h | 24 +++++++++++----------- .../include/gmock/internal/gmock-internal-utils.h | 9 ++++---- googlemock/test/gmock-more-actions_test.cc | 1 - googletest/docs/advanced.md | 7 ++++--- googletest/include/gtest/gtest.h | 5 +++-- googletest/include/gtest/internal/gtest-internal.h | 7 +++---- googletest/include/gtest/internal/gtest-port.h | 11 ---------- 7 files changed, 26 insertions(+), 38 deletions(-) diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index ad16687..bf9eb20 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -2068,15 +2068,15 @@ class ContainerEqMatcher { typedef typename View::type StlContainer; typedef typename View::const_reference StlContainerReference; + static_assert(!std::is_const::value, + "Container type must not be const"); + static_assert(!std::is_reference::value, + "Container type must not be a reference"); + // We make a copy of expected in case the elements in it are modified // after this matcher is created. explicit ContainerEqMatcher(const Container& expected) - : expected_(View::Copy(expected)) { - // Makes sure the user doesn't instantiate this class template - // with a const or reference type. - (void)testing::StaticAssertTypeEq(); - } + : expected_(View::Copy(expected)) {} void DescribeTo(::std::ostream* os) const { *os << "equals "; @@ -2243,15 +2243,15 @@ class PointwiseMatcher { typedef typename RhsView::type RhsStlContainer; typedef typename RhsStlContainer::value_type RhsValue; + static_assert(!std::is_const::value, + "RhsContainer type must not be const"); + static_assert(!std::is_reference::value, + "RhsContainer type must not be a reference"); + // Like ContainerEq, we make a copy of rhs in case the elements in // it are modified after this matcher is created. PointwiseMatcher(const TupleMatcher& tuple_matcher, const RhsContainer& rhs) - : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) { - // Makes sure the user doesn't instantiate this class template - // with a const or reference type. - (void)testing::StaticAssertTypeEq(); - } + : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {} template operator Matcher() const { diff --git a/googlemock/include/gmock/internal/gmock-internal-utils.h b/googlemock/include/gmock/internal/gmock-internal-utils.h index e05b883..fdc049c 100644 --- a/googlemock/include/gmock/internal/gmock-internal-utils.h +++ b/googlemock/include/gmock/internal/gmock-internal-utils.h @@ -384,9 +384,8 @@ class StlContainerView { typedef const type& const_reference; static const_reference ConstReference(const RawContainer& container) { - // Ensures that RawContainer is not a const type. - testing::StaticAssertTypeEq< - RawContainer, typename std::remove_const::type>(); + static_assert(!std::is_const::value, + "RawContainer type must not be const"); return container; } static type Copy(const RawContainer& container) { return container; } @@ -406,8 +405,8 @@ class StlContainerView { typedef const type const_reference; static const_reference ConstReference(const Element (&array)[N]) { - // Ensures that Element is not a const type. - testing::StaticAssertTypeEq(); + static_assert(std::is_same::value, + "Element type must not be const"); return type(array, N, RelationToSourceReference()); } static type Copy(const Element (&array)[N]) { diff --git a/googlemock/test/gmock-more-actions_test.cc b/googlemock/test/gmock-more-actions_test.cc index b4e0fc3..97ec5cf 100644 --- a/googlemock/test/gmock-more-actions_test.cc +++ b/googlemock/test/gmock-more-actions_test.cc @@ -57,7 +57,6 @@ using testing::ReturnPointee; using testing::SaveArg; using testing::SaveArgPointee; using testing::SetArgReferee; -using testing::StaticAssertTypeEq; using testing::Unused; using testing::WithArg; using testing::WithoutArgs; diff --git a/googletest/docs/advanced.md b/googletest/docs/advanced.md index 51005e9..3e5f779 100644 --- a/googletest/docs/advanced.md +++ b/googletest/docs/advanced.md @@ -464,9 +464,10 @@ You can call the function to assert that types `T1` and `T2` are the same. The function does nothing if the assertion is satisfied. If the types are different, the function call will -fail to compile, and the compiler error message will likely (depending on the -compiler) show you the actual values of `T1` and `T2`. This is mainly useful -inside template code. +fail to compile, the compiler error message will say that +`type1 and type2 are not the same type` and most likely (depending on the compiler) +show you the actual values of `T1` and `T2`. This is mainly useful inside +template code. **Caveat**: When used inside a member function of a class template or a function template, `StaticAssertTypeEq()` is effective only if the function is diff --git a/googletest/include/gtest/gtest.h b/googletest/include/gtest/gtest.h index 8e125a4..dbe5b1c 100644 --- a/googletest/include/gtest/gtest.h +++ b/googletest/include/gtest/gtest.h @@ -2297,8 +2297,9 @@ class GTEST_API_ ScopedTrace { // // to cause a compiler error. template -bool StaticAssertTypeEq() { - (void)internal::StaticAssertTypeEqHelper(); +constexpr bool StaticAssertTypeEq() noexcept { + static_assert(std::is_same::value, + "type1 and type2 are not the same type"); return true; } diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index 22127c7..82ec6e2 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -1069,10 +1069,9 @@ class NativeArray { } private: - enum { - kCheckTypeIsNotConstOrAReference = StaticAssertTypeEqHelper< - Element, GTEST_REMOVE_REFERENCE_AND_CONST_(Element)>::value - }; + static_assert(!std::is_const::value, "Type must not be const"); + static_assert(!std::is_reference::value, + "Type must not be a reference"); // Initializes this object with a copy of the input. void InitCopy(const Element* array, size_t a_size) { diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index 0813adc..063fcb1 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -856,17 +856,6 @@ class Secret; // expression is false, compiler will issue an error containing this identifier. #define GTEST_COMPILE_ASSERT_(expr, msg) static_assert(expr, #msg) -// StaticAssertTypeEqHelper is used by StaticAssertTypeEq defined in gtest.h. -// -// This template is declared, but intentionally undefined. -template -struct StaticAssertTypeEqHelper; - -template -struct StaticAssertTypeEqHelper { - enum { value = true }; -}; - // Evaluates to the number of elements in 'array'. #define GTEST_ARRAY_SIZE_(array) (sizeof(array) / sizeof(array[0])) -- cgit v0.12