Merge pull request #2393 from kuzkry:custom-type-traits-remove_const

PiperOrigin-RevId: 264652890
This commit is contained in:
Xiaoyi Zhang 2019-08-21 17:13:34 -04:00
commit fb49e6c164
4 changed files with 27 additions and 74 deletions

View File

@ -1610,8 +1610,9 @@ class PointeeMatcher {
template <typename Pointer> template <typename Pointer>
class Impl : public MatcherInterface<Pointer> { class Impl : public MatcherInterface<Pointer> {
public: public:
typedef typename PointeeOf<GTEST_REMOVE_CONST_( // NOLINT typedef
GTEST_REMOVE_REFERENCE_(Pointer))>::type Pointee; typename PointeeOf<typename std::remove_const<GTEST_REMOVE_REFERENCE_(
Pointer)>::type>::type Pointee;
explicit Impl(const InnerMatcher& matcher) explicit Impl(const InnerMatcher& matcher)
: matcher_(MatcherCast<const Pointee&>(matcher)) {} : matcher_(MatcherCast<const Pointee&>(matcher)) {}
@ -1749,8 +1750,8 @@ class FieldMatcher {
// FIXME: The dispatch on std::is_pointer was introduced as a workaround for // FIXME: The dispatch on std::is_pointer was introduced as a workaround for
// a compiler bug, and can now be removed. // a compiler bug, and can now be removed.
return MatchAndExplainImpl( return MatchAndExplainImpl(
typename std::is_pointer<GTEST_REMOVE_CONST_(T)>::type(), value, typename std::is_pointer<typename std::remove_const<T>::type>::type(),
listener); value, listener);
} }
private: private:
@ -1816,8 +1817,8 @@ class PropertyMatcher {
template <typename T> template <typename T>
bool MatchAndExplain(const T&value, MatchResultListener* listener) const { bool MatchAndExplain(const T&value, MatchResultListener* listener) const {
return MatchAndExplainImpl( return MatchAndExplainImpl(
typename std::is_pointer<GTEST_REMOVE_CONST_(T)>::type(), value, typename std::is_pointer<typename std::remove_const<T>::type>::type(),
listener); value, listener);
} }
private: private:
@ -2093,9 +2094,8 @@ class ContainerEqMatcher {
template <typename LhsContainer> template <typename LhsContainer>
bool MatchAndExplain(const LhsContainer& lhs, bool MatchAndExplain(const LhsContainer& lhs,
MatchResultListener* listener) const { MatchResultListener* listener) const {
// GTEST_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug typedef internal::StlContainerView<
// that causes LhsContainer to be a const type sometimes. typename std::remove_const<LhsContainer>::type>
typedef internal::StlContainerView<GTEST_REMOVE_CONST_(LhsContainer)>
LhsView; LhsView;
typedef typename LhsView::type LhsStlContainer; typedef typename LhsView::type LhsStlContainer;
StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs); StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
@ -4034,12 +4034,12 @@ BeginEndDistanceIs(const DistanceMatcher& distance_matcher) {
// values that are included in one container but not the other. (Duplicate // values that are included in one container but not the other. (Duplicate
// values and order differences are not explained.) // values and order differences are not explained.)
template <typename Container> template <typename Container>
inline PolymorphicMatcher<internal::ContainerEqMatcher< // NOLINT inline PolymorphicMatcher<internal::ContainerEqMatcher<
GTEST_REMOVE_CONST_(Container)> > typename std::remove_const<Container>::type>>
ContainerEq(const Container& rhs) { ContainerEq(const Container& rhs) {
// This following line is for working around a bug in MSVC 8.0, // This following line is for working around a bug in MSVC 8.0,
// which causes Container to be a const type sometimes. // which causes Container to be a const type sometimes.
typedef GTEST_REMOVE_CONST_(Container) RawContainer; typedef typename std::remove_const<Container>::type RawContainer;
return MakePolymorphicMatcher( return MakePolymorphicMatcher(
internal::ContainerEqMatcher<RawContainer>(rhs)); internal::ContainerEqMatcher<RawContainer>(rhs));
} }
@ -4072,12 +4072,12 @@ WhenSorted(const ContainerMatcher& container_matcher) {
// LHS container and the RHS container respectively. // LHS container and the RHS container respectively.
template <typename TupleMatcher, typename Container> template <typename TupleMatcher, typename Container>
inline internal::PointwiseMatcher<TupleMatcher, inline internal::PointwiseMatcher<TupleMatcher,
GTEST_REMOVE_CONST_(Container)> typename std::remove_const<Container>::type>
Pointwise(const TupleMatcher& tuple_matcher, const Container& rhs) { Pointwise(const TupleMatcher& tuple_matcher, const Container& rhs) {
// This following line is for working around a bug in MSVC 8.0, // This following line is for working around a bug in MSVC 8.0,
// which causes Container to be a const type sometimes (e.g. when // which causes Container to be a const type sometimes (e.g. when
// rhs is a const int[]).. // rhs is a const int[])..
typedef GTEST_REMOVE_CONST_(Container) RawContainer; typedef typename std::remove_const<Container>::type RawContainer;
return internal::PointwiseMatcher<TupleMatcher, RawContainer>( return internal::PointwiseMatcher<TupleMatcher, RawContainer>(
tuple_matcher, rhs); tuple_matcher, rhs);
} }
@ -4105,14 +4105,15 @@ inline internal::PointwiseMatcher<TupleMatcher, std::vector<T> > Pointwise(
template <typename Tuple2Matcher, typename RhsContainer> template <typename Tuple2Matcher, typename RhsContainer>
inline internal::UnorderedElementsAreArrayMatcher< inline internal::UnorderedElementsAreArrayMatcher<
typename internal::BoundSecondMatcher< typename internal::BoundSecondMatcher<
Tuple2Matcher, typename internal::StlContainerView<GTEST_REMOVE_CONST_( Tuple2Matcher,
RhsContainer)>::type::value_type> > typename internal::StlContainerView<
typename std::remove_const<RhsContainer>::type>::type::value_type>>
UnorderedPointwise(const Tuple2Matcher& tuple2_matcher, UnorderedPointwise(const Tuple2Matcher& tuple2_matcher,
const RhsContainer& rhs_container) { const RhsContainer& rhs_container) {
// This following line is for working around a bug in MSVC 8.0, // This following line is for working around a bug in MSVC 8.0,
// which causes RhsContainer to be a const type sometimes (e.g. when // which causes RhsContainer to be a const type sometimes (e.g. when
// rhs_container is a const int[]). // rhs_container is a const int[]).
typedef GTEST_REMOVE_CONST_(RhsContainer) RawRhsContainer; typedef typename std::remove_const<RhsContainer>::type RawRhsContainer;
// RhsView allows the same code to handle RhsContainer being a // RhsView allows the same code to handle RhsContainer being a
// STL-style container and it being a native C-style array. // STL-style container and it being a native C-style array.

View File

@ -402,8 +402,8 @@ class StlContainerView {
static const_reference ConstReference(const RawContainer& container) { static const_reference ConstReference(const RawContainer& container) {
// Ensures that RawContainer is not a const type. // Ensures that RawContainer is not a const type.
testing::StaticAssertTypeEq<RawContainer, testing::StaticAssertTypeEq<
GTEST_REMOVE_CONST_(RawContainer)>(); RawContainer, typename std::remove_const<RawContainer>::type>();
return container; return container;
} }
static type Copy(const RawContainer& container) { return container; } static type Copy(const RawContainer& container) { return container; }
@ -413,7 +413,7 @@ class StlContainerView {
template <typename Element, size_t N> template <typename Element, size_t N>
class StlContainerView<Element[N]> { class StlContainerView<Element[N]> {
public: public:
typedef GTEST_REMOVE_CONST_(Element) RawElement; typedef typename std::remove_const<Element>::type RawElement;
typedef internal::NativeArray<RawElement> type; typedef internal::NativeArray<RawElement> type;
// NativeArray<T> can represent a native array either by value or by // NativeArray<T> can represent a native array either by value or by
// reference (selected by a constructor argument), so 'const type' // reference (selected by a constructor argument), so 'const type'
@ -437,8 +437,8 @@ class StlContainerView<Element[N]> {
template <typename ElementPointer, typename Size> template <typename ElementPointer, typename Size>
class StlContainerView< ::std::tuple<ElementPointer, Size> > { class StlContainerView< ::std::tuple<ElementPointer, Size> > {
public: public:
typedef GTEST_REMOVE_CONST_( typedef typename std::remove_const<
typename internal::PointeeOf<ElementPointer>::type) RawElement; typename internal::PointeeOf<ElementPointer>::type>::type RawElement;
typedef internal::NativeArray<RawElement> type; typedef internal::NativeArray<RawElement> type;
typedef const type const_reference; typedef const type const_reference;

View File

@ -869,30 +869,9 @@ struct RemoveReference<T&> { typedef T type; }; // NOLINT
#define GTEST_REMOVE_REFERENCE_(T) \ #define GTEST_REMOVE_REFERENCE_(T) \
typename ::testing::internal::RemoveReference<T>::type typename ::testing::internal::RemoveReference<T>::type
// Removes const from a type if it is a const type, otherwise leaves
// it unchanged. This is the same as tr1::remove_const, which is not
// widely available yet.
template <typename T>
struct RemoveConst { typedef T type; }; // NOLINT
template <typename T>
struct RemoveConst<const T> { typedef T type; }; // NOLINT
// MSVC 8.0, Sun C++, and IBM XL C++ have a bug which causes the above
// definition to fail to remove the const in 'const int[3]' and 'const
// char[3][4]'. The following specialization works around the bug.
template <typename T, size_t N>
struct RemoveConst<const T[N]> {
typedef typename RemoveConst<T>::type type[N];
};
// A handy wrapper around RemoveConst that works when the argument
// T depends on template parameters.
#define GTEST_REMOVE_CONST_(T) \
typename ::testing::internal::RemoveConst<T>::type
// Turns const U&, U&, const U, and U all into U. // Turns const U&, U&, const U, and U all into U.
#define GTEST_REMOVE_REFERENCE_AND_CONST_(T) \ #define GTEST_REMOVE_REFERENCE_AND_CONST_(T) \
GTEST_REMOVE_CONST_(GTEST_REMOVE_REFERENCE_(T)) typename std::remove_const<GTEST_REMOVE_REFERENCE_(T)>::type
// IsAProtocolMessage<T>::value is a compile-time bool constant that's // IsAProtocolMessage<T>::value is a compile-time bool constant that's
// true if T is type proto2::Message or a subclass of it. // true if T is type proto2::Message or a subclass of it.

View File

@ -61,9 +61,10 @@ TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) {
#include <time.h> #include <time.h>
#include <map> #include <map>
#include <vector>
#include <ostream> #include <ostream>
#include <type_traits>
#include <unordered_set> #include <unordered_set>
#include <vector>
#include "gtest/gtest-spi.h" #include "gtest/gtest-spi.h"
#include "src/gtest-internal-inl.h" #include "src/gtest-internal-inl.h"
@ -262,7 +263,6 @@ using testing::internal::OsStackTraceGetterInterface;
using testing::internal::ParseInt32Flag; using testing::internal::ParseInt32Flag;
using testing::internal::RelationToSourceCopy; using testing::internal::RelationToSourceCopy;
using testing::internal::RelationToSourceReference; using testing::internal::RelationToSourceReference;
using testing::internal::RemoveConst;
using testing::internal::RemoveReference; using testing::internal::RemoveReference;
using testing::internal::ShouldRunTestOnShard; using testing::internal::ShouldRunTestOnShard;
using testing::internal::ShouldShard; using testing::internal::ShouldShard;
@ -7135,33 +7135,6 @@ TEST(RemoveReferenceTest, MacroVersion) {
TestGTestRemoveReference<const char, const char&>(); TestGTestRemoveReference<const char, const char&>();
} }
// Tests that RemoveConst does not affect non-const types.
TEST(RemoveConstTest, DoesNotAffectNonConstType) {
CompileAssertTypesEqual<int, RemoveConst<int>::type>();
CompileAssertTypesEqual<char&, RemoveConst<char&>::type>();
}
// Tests that RemoveConst removes const from const types.
TEST(RemoveConstTest, RemovesConst) {
CompileAssertTypesEqual<int, RemoveConst<const int>::type>();
CompileAssertTypesEqual<char[2], RemoveConst<const char[2]>::type>();
CompileAssertTypesEqual<char[2][3], RemoveConst<const char[2][3]>::type>();
}
// Tests GTEST_REMOVE_CONST_.
template <typename T1, typename T2>
void TestGTestRemoveConst() {
CompileAssertTypesEqual<T1, GTEST_REMOVE_CONST_(T2)>();
}
TEST(RemoveConstTest, MacroVersion) {
TestGTestRemoveConst<int, int>();
TestGTestRemoveConst<double&, double&>();
TestGTestRemoveConst<char, const char>();
}
// Tests GTEST_REMOVE_REFERENCE_AND_CONST_. // Tests GTEST_REMOVE_REFERENCE_AND_CONST_.
template <typename T1, typename T2> template <typename T1, typename T2>