Skip to content

Commit

Permalink
remove unnecessary StaticAssertTypeEq and its helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzkry committed Aug 16, 2019
1 parent 9b70406 commit 833b8d5
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 153 deletions.
24 changes: 12 additions & 12 deletions googlemock/include/gmock/gmock-matchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2071,15 +2071,15 @@ class ContainerEqMatcher {
typedef typename View::type StlContainer;
typedef typename View::const_reference StlContainerReference;

static_assert(!std::is_const<Container>::value,
"Container type must not be const");
static_assert(!std::is_reference<Container>::value,
"Container type must not be 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<Container,
GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>();
}
: expected_(View::Copy(expected)) {}

void DescribeTo(::std::ostream* os) const {
*os << "equals ";
Expand Down Expand Up @@ -2247,15 +2247,15 @@ class PointwiseMatcher {
typedef typename RhsView::type RhsStlContainer;
typedef typename RhsStlContainer::value_type RhsValue;

static_assert(!std::is_const<RhsContainer>::value,
"RhsContainer type must not be const");
static_assert(!std::is_reference<RhsContainer>::value,
"RhsContainer type must not be 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<RhsContainer,
GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>();
}
: tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {}

template <typename LhsContainer>
operator Matcher<LhsContainer>() const {
Expand Down
9 changes: 4 additions & 5 deletions googlemock/include/gmock/internal/gmock-internal-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,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,
GTEST_REMOVE_CONST_(RawContainer)>();
static_assert(!std::is_const<RawContainer>::value,
"RawContainer type must not be const");
return container;
}
static type Copy(const RawContainer& container) { return container; }
Expand All @@ -446,8 +445,8 @@ class StlContainerView<Element[N]> {
typedef const type const_reference;

static const_reference ConstReference(const Element (&array)[N]) {
// Ensures that Element is not a const type.
testing::StaticAssertTypeEq<Element, RawElement>();
static_assert(std::is_same<Element, RawElement>::value,
"Element type must not be const");
return type(array, N, RelationToSourceReference());
}
static type Copy(const Element (&array)[N]) {
Expand Down
15 changes: 8 additions & 7 deletions googlemock/test/gmock-generated-actions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <memory>
#include <sstream>
#include <string>
#include <type_traits>
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand All @@ -55,7 +56,6 @@ using testing::Invoke;
using testing::Return;
using testing::ReturnNew;
using testing::SetArgPointee;
using testing::StaticAssertTypeEq;
using testing::Unused;

// For suppressing compiler warnings on conversion possibly losing precision.
Expand Down Expand Up @@ -461,7 +461,7 @@ TEST(ActionMacroTest, WorksWhenReturningVoid) {
// Tests that the body of ACTION() can reference the type of the
// argument.
ACTION(IncrementArg2) {
StaticAssertTypeEq<int*, arg2_type>();
static_assert(std::is_same<int*, arg2_type>::value, "");
arg2_type temp = arg2;
(*temp)++;
}
Expand All @@ -476,7 +476,8 @@ TEST(ActionMacroTest, CanReferenceArgumentType) {
// Tests that the body of ACTION() can reference the argument tuple
// via args_type and args.
ACTION(Sum2) {
StaticAssertTypeEq<std::tuple<int, char, int*>, args_type>();
static_assert(std::is_same<std::tuple<int, char, int*>, args_type>::value,
"");
args_type args_copy = args;
return std::get<0>(args_copy) + std::get<1>(args_copy);
}
Expand All @@ -492,7 +493,7 @@ TEST(ActionMacroTest, CanReferenceArgumentTuple) {
int Dummy(bool flag) { return flag? 1 : 0; }

ACTION(InvokeDummy) {
StaticAssertTypeEq<int(bool), function_type>();
static_assert(std::is_same<int(bool), function_type>::value, "");
function_type* fp = &Dummy;
return (*fp)(true);
}
Expand All @@ -506,7 +507,7 @@ TEST(ActionMacroTest, CanReferenceMockFunctionType) {
// Tests that the body of ACTION() can reference the mock function's
// return type.
ACTION(InvokeDummy2) {
StaticAssertTypeEq<int, return_type>();
static_assert(std::is_same<int, return_type>::value, "");
return_type result = Dummy(true);
return result;
}
Expand All @@ -519,7 +520,7 @@ TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {

// Tests that ACTION() works for arguments passed by const reference.
ACTION(ReturnAddrOfConstBoolReferenceArg) {
StaticAssertTypeEq<const bool&, arg1_type>();
static_assert(std::is_same<const bool&, arg1_type>::value, "");
return &arg1;
}

Expand All @@ -531,7 +532,7 @@ TEST(ActionMacroTest, WorksForConstReferenceArg) {

// Tests that ACTION() works for arguments passed by non-const reference.
ACTION(ReturnAddrOfIntReferenceArg) {
StaticAssertTypeEq<int&, arg0_type>();
static_assert(std::is_same<int&, arg0_type>::value, "");
return &arg0;
}

Expand Down
12 changes: 6 additions & 6 deletions googlemock/test/gmock-generated-matchers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <set>
#include <sstream>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -85,7 +86,6 @@ using testing::Not;
using testing::Pointee;
using testing::PrintToString;
using testing::Ref;
using testing::StaticAssertTypeEq;
using testing::StrEq;
using testing::Value;
using testing::internal::ElementsAreArrayMatcher;
Expand Down Expand Up @@ -653,12 +653,12 @@ TEST(MatcherMacroTest, CanExplainMatchResult) {
// value being matched.

MATCHER(IsEmptyString, "") {
StaticAssertTypeEq< ::std::string, arg_type>();
static_assert(std::is_same<::std::string, arg_type>::value, "");
return arg == "";
}

MATCHER(IsEmptyStringByRef, "") {
StaticAssertTypeEq<const ::std::string&, arg_type>();
static_assert(std::is_same<const ::std::string&, arg_type>::value, "");
return arg == "";
}

Expand Down Expand Up @@ -755,9 +755,9 @@ TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
// types.

MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
StaticAssertTypeEq<int, foo_type>();
StaticAssertTypeEq<long, bar_type>(); // NOLINT
StaticAssertTypeEq<char, baz_type>();
static_assert(std::is_same<int, foo_type>::value, "");
static_assert(std::is_same<long, bar_type>::value, "");
static_assert(std::is_same<char, baz_type>::value, "");
return arg == 0;
}

Expand Down
61 changes: 38 additions & 23 deletions googlemock/test/gmock-internal-utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
#include <stdlib.h>
#include <map>
#include <memory>
#include <string>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
#include "gmock/gmock.h"
Expand Down Expand Up @@ -609,10 +609,14 @@ TEST(OnCallTest, LogsAnythingArgument) {
// Tests StlContainerView.

TEST(StlContainerViewTest, WorksForStlContainer) {
StaticAssertTypeEq<std::vector<int>,
StlContainerView<std::vector<int> >::type>();
StaticAssertTypeEq<const std::vector<double>&,
StlContainerView<std::vector<double> >::const_reference>();
static_assert(std::is_same<std::vector<int>,
StlContainerView<std::vector<int> >::type>::value,
"");
static_assert(
std::is_same<
const std::vector<double>&,
StlContainerView<std::vector<double> >::const_reference>::value,
"");

typedef std::vector<char> Chars;
Chars v1;
Expand All @@ -625,15 +629,19 @@ TEST(StlContainerViewTest, WorksForStlContainer) {
}

TEST(StlContainerViewTest, WorksForStaticNativeArray) {
StaticAssertTypeEq<NativeArray<int>,
StlContainerView<int[3]>::type>();
StaticAssertTypeEq<NativeArray<double>,
StlContainerView<const double[4]>::type>();
StaticAssertTypeEq<NativeArray<char[3]>,
StlContainerView<const char[2][3]>::type>();

StaticAssertTypeEq<const NativeArray<int>,
StlContainerView<int[2]>::const_reference>();
static_assert(
std::is_same<NativeArray<int>, StlContainerView<int[3]>::type>::value,
"");
static_assert(std::is_same<NativeArray<double>,
StlContainerView<const double[4]>::type>::value,
"");
static_assert(std::is_same<NativeArray<char[3]>,
StlContainerView<const char[2][3]>::type>::value,
"");

static_assert(std::is_same<const NativeArray<int>,
StlContainerView<int[2]>::const_reference>::value,
"");

int a1[3] = { 0, 1, 2 };
NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1);
Expand All @@ -652,15 +660,22 @@ TEST(StlContainerViewTest, WorksForStaticNativeArray) {
}

TEST(StlContainerViewTest, WorksForDynamicNativeArray) {
StaticAssertTypeEq<NativeArray<int>,
StlContainerView<std::tuple<const int*, size_t> >::type>();
StaticAssertTypeEq<
NativeArray<double>,
StlContainerView<std::tuple<std::shared_ptr<double>, int> >::type>();

StaticAssertTypeEq<
const NativeArray<int>,
StlContainerView<std::tuple<const int*, int> >::const_reference>();
static_assert(
std::is_same<
NativeArray<int>,
StlContainerView<std::tuple<const int*, size_t> >::type>::value,
"");
static_assert(
std::is_same<NativeArray<double>,
StlContainerView<
std::tuple<std::shared_ptr<double>, int> >::type>::value,
"");

static_assert(
std::is_same<const NativeArray<int>,
StlContainerView<
std::tuple<const int*, int> >::const_reference>::value,
"");

int a1[3] = { 0, 1, 2 };
const int* const p1 = a1;
Expand Down
2 changes: 1 addition & 1 deletion googlemock/test/gmock-more-actions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <memory>
#include <sstream>
#include <string>
#include <type_traits>
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand All @@ -57,7 +58,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;
Expand Down
4 changes: 3 additions & 1 deletion googlemock/test/gmock-spec-builders_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <ostream> // NOLINT
#include <sstream>
#include <string>
#include <type_traits>

#include "gmock/gmock.h"
#include "gmock/internal/gmock-port.h"
Expand Down Expand Up @@ -1511,7 +1512,8 @@ TEST(ExpectationTest, AssignmentWorks) {
// Tests ExpectationSet.

TEST(ExpectationSetTest, MemberTypesAreCorrect) {
::testing::StaticAssertTypeEq<Expectation, ExpectationSet::value_type>();
static_assert(std::is_same<Expectation, ExpectationSet::value_type>::value,
"Member types aren't corrent");
}

TEST(ExpectationSetTest, ConstructorsWork) {
Expand Down
37 changes: 0 additions & 37 deletions googletest/include/gtest/gtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -2265,43 +2265,6 @@ class GTEST_API_ ScopedTrace {
::testing::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\
__FILE__, __LINE__, (message))


// Compile-time assertion for type equality.
// StaticAssertTypeEq<type1, type2>() compiles if type1 and type2 are
// the same type. The value it returns is not interesting.
//
// Instead of making StaticAssertTypeEq a class template, we make it a
// function template that invokes a helper class template. This
// prevents a user from misusing StaticAssertTypeEq<T1, T2> by
// defining objects of that type.
//
// CAVEAT:
//
// When used inside a method of a class template,
// StaticAssertTypeEq<T1, T2>() is effective ONLY IF the method is
// instantiated. For example, given:
//
// template <typename T> class Foo {
// public:
// void Bar() { testing::StaticAssertTypeEq<int, T>(); }
// };
//
// the code:
//
// void Test1() { Foo<bool> foo; }
//
// will NOT generate a compiler error, as Foo<bool>::Bar() is never
// actually instantiated. Instead, you need:
//
// void Test2() { Foo<bool> foo; foo.Bar(); }
//
// to cause a compiler error.
template <typename T1, typename T2>
bool StaticAssertTypeEq() {
(void)internal::StaticAssertTypeEqHelper<T1, T2>();
return true;
}

// Defines a test.
//
// The first parameter is the name of the test suite, and the second
Expand Down
7 changes: 3 additions & 4 deletions googletest/include/gtest/internal/gtest-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1120,10 +1120,9 @@ class NativeArray {
}

private:
enum {
kCheckTypeIsNotConstOrAReference = StaticAssertTypeEqHelper<
Element, GTEST_REMOVE_REFERENCE_AND_CONST_(Element)>::value
};
static_assert(!std::is_const<Element>::value, "Type must not be const");
static_assert(!std::is_reference<Element>::value,
"Type must not be reference");

// Initializes this object with a copy of the input.
void InitCopy(const Element* array, size_t a_size) {
Expand Down
11 changes: 0 additions & 11 deletions googletest/include/gtest/internal/gtest-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -858,17 +858,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 <typename T1, typename T2>
struct StaticAssertTypeEqHelper;

template <typename T>
struct StaticAssertTypeEqHelper<T, T> {
enum { value = true };
};

// Evaluates to the number of elements in 'array'.
#define GTEST_ARRAY_SIZE_(array) (sizeof(array) / sizeof(array[0]))

Expand Down
Loading

0 comments on commit 833b8d5

Please sign in to comment.