aboutsummaryrefslogtreecommitdiffstats
path: root/googletest/include/gtest/gtest-matchers.h
diff options
context:
space:
mode:
Diffstat (limited to 'googletest/include/gtest/gtest-matchers.h')
-rw-r--r--googletest/include/gtest/gtest-matchers.h26
1 files changed, 15 insertions, 11 deletions
diff --git a/googletest/include/gtest/gtest-matchers.h b/googletest/include/gtest/gtest-matchers.h
index 128fdb2f..846b9455 100644
--- a/googletest/include/gtest/gtest-matchers.h
+++ b/googletest/include/gtest/gtest-matchers.h
@@ -555,13 +555,12 @@ class PolymorphicMatcher {
Impl impl_;
};
-// Creates a matcher from its implementation. This is easier to use
-// than the Matcher<T> constructor as it doesn't require you to
-// explicitly write the template argument, e.g.
+// Creates a matcher from its implementation.
+// DEPRECATED: Especially in the generic code, prefer:
+// Matcher<T>(new MyMatcherImpl<const T&>(...));
//
-// MakeMatcher(foo);
-// vs
-// Matcher<const string&>(foo);
+// MakeMatcher may create a Matcher that accepts its argument by value, which
+// leads to unnecessary copies & lack of support for non-copyable types.
template <typename T>
inline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) {
return Matcher<T>(impl);
@@ -596,25 +595,30 @@ class ComparisonBase {
explicit ComparisonBase(const Rhs& rhs) : rhs_(rhs) {}
template <typename Lhs>
operator Matcher<Lhs>() const {
- return MakeMatcher(new Impl<Lhs>(rhs_));
+ return Matcher<Lhs>(new Impl<const Lhs&>(rhs_));
}
private:
- template <typename Lhs>
+ template <typename T>
+ static const T& Unwrap(const T& v) { return v; }
+ template <typename T>
+ static const T& Unwrap(std::reference_wrapper<T> v) { return v; }
+
+ template <typename Lhs, typename = Rhs>
class Impl : public MatcherInterface<Lhs> {
public:
explicit Impl(const Rhs& rhs) : rhs_(rhs) {}
bool MatchAndExplain(Lhs lhs,
MatchResultListener* /* listener */) const override {
- return Op()(lhs, rhs_);
+ return Op()(lhs, Unwrap(rhs_));
}
void DescribeTo(::std::ostream* os) const override {
*os << D::Desc() << " ";
- UniversalPrint(rhs_, os);
+ UniversalPrint(Unwrap(rhs_), os);
}
void DescribeNegationTo(::std::ostream* os) const override {
*os << D::NegatedDesc() << " ";
- UniversalPrint(rhs_, os);
+ UniversalPrint(Unwrap(rhs_), os);
}
private: