Support std::reference_wapper as a smart pointer type in gmock

PiperOrigin-RevId: 413963503
Change-Id: I33440780bf087698f4e4325399227405885430b3
This commit is contained in:
Abseil Team 2021-12-03 10:13:10 -08:00 committed by Copybara-Service
parent 1b2606425c
commit d61d4d8e64
2 changed files with 13 additions and 0 deletions

View File

@ -76,6 +76,13 @@ template <typename Pointer>
inline const typename Pointer::element_type* GetRawPointer(const Pointer& p) {
return p.get();
}
// This overload version is for std::reference_wrapper, which does not work with
// the overload above, as it does not have an `element_type`.
template <typename Element>
inline const Element* GetRawPointer(const std::reference_wrapper<Element>& r) {
return &r.get();
}
// This overloaded version is for the raw pointer case.
template <typename Element>
inline Element* GetRawPointer(Element* p) { return p; }

View File

@ -140,6 +140,12 @@ TEST(GetRawPointerTest, WorksForRawPointers) {
EXPECT_EQ(&n, GetRawPointer(&n));
}
TEST(GetRawPointerTest, WorksForStdReferenceWrapper) {
int n = 1;
EXPECT_EQ(&n, GetRawPointer(std::ref(n)));
EXPECT_EQ(&n, GetRawPointer(std::cref(n)));
}
// Tests KindOf<T>.
class Base {};