Skip to content

Commit

Permalink
Cast mutable lvalue references to const from testing::ResultOf
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 716343482
Change-Id: I125bc4725886958d026c88f3902a8289e476598b
  • Loading branch information
Abseil Team authored and copybara-github committed Jan 16, 2025
1 parent e4ece48 commit 2b6b042
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions googlemock/include/gmock/gmock-matchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2281,14 +2281,17 @@ class ResultOfMatcher {
class Impl : public MatcherInterface<T> {
using ResultType = decltype(CallableTraits<Callable>::template Invoke<T>(
std::declval<CallableStorageType>(), std::declval<T>()));
using InnerType = std::conditional_t<
std::is_lvalue_reference<ResultType>::value,
const typename std::remove_reference<ResultType>::type&, ResultType>;

public:
template <typename M>
Impl(const std::string& result_description,
const CallableStorageType& callable, const M& matcher)
: result_description_(result_description),
callable_(callable),
matcher_(MatcherCast<ResultType>(matcher)) {}
matcher_(MatcherCast<InnerType>(matcher)) {}

void DescribeTo(::std::ostream* os) const override {
if (result_description_.empty()) {
Expand Down Expand Up @@ -2318,7 +2321,7 @@ class ResultOfMatcher {
// takes a non-const reference as argument.
// Also, specifying template argument explicitly is needed because T could
// be a non-const reference (e.g. Matcher<Uncopyable&>).
ResultType result =
InnerType result =
CallableTraits<Callable>::template Invoke<T>(callable_, obj);
return MatchPrintAndExplain(result, matcher_, listener);
}
Expand All @@ -2331,7 +2334,7 @@ class ResultOfMatcher {
// use stateful callables with ResultOf(), which doesn't guarantee
// how many times the callable will be invoked.
mutable CallableStorageType callable_;
const Matcher<ResultType> matcher_;
const Matcher<InnerType> matcher_;
}; // class Impl

const std::string result_description_;
Expand Down

0 comments on commit 2b6b042

Please sign in to comment.