Skip to content

Commit 137b6e2

Browse files
Abseil Teamcopybara-github
authored andcommitted
Terse printing of std::reference_wrapper hides pointer
This matches the intention and documentation of terse printing which generally avoids printing the pointer. PiperOrigin-RevId: 481178950 Change-Id: I27039dac1870934d2d5b212e2cc7e97ab82c5b34
1 parent 67174c7 commit 137b6e2

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

googletest/include/gtest/gtest-printers.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,13 @@ class UniversalTersePrinter<T&> {
891891
UniversalPrint(value, os);
892892
}
893893
};
894+
template <typename T>
895+
class UniversalTersePrinter<std::reference_wrapper<T>> {
896+
public:
897+
static void Print(std::reference_wrapper<T> value, ::std::ostream* os) {
898+
UniversalTersePrinter<T>::Print(value.get(), os);
899+
}
900+
};
894901
template <typename T, size_t N>
895902
class UniversalTersePrinter<T[N]> {
896903
public:

googletest/test/googletest-printers-test.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <cstring>
3838
#include <deque>
3939
#include <forward_list>
40+
#include <functional>
4041
#include <limits>
4142
#include <list>
4243
#include <map>
@@ -193,6 +194,11 @@ OutputStream& operator<<(OutputStream& os,
193194
return os;
194195
}
195196

197+
struct StreamableInLocal {};
198+
void operator<<(::std::ostream& os, const StreamableInLocal& /* x */) {
199+
os << "StreamableInLocal";
200+
}
201+
196202
// A user-defined streamable but recursively-defined container type in
197203
// a user namespace, it mimics therefore std::filesystem::path or
198204
// boost::filesystem::path.
@@ -1604,6 +1610,23 @@ TEST(PrintToStringTest, ContainsNonLatin) {
16041610
"\n As Text: \"From ä — ẑ\"");
16051611
}
16061612

1613+
TEST(PrintToStringTest, PrintStreamableInLocal) {
1614+
EXPECT_STREQ("StreamableInLocal",
1615+
PrintToString(foo::StreamableInLocal()).c_str());
1616+
}
1617+
1618+
TEST(PrintToStringTest, PrintReferenceToStreamableInLocal) {
1619+
foo::StreamableInLocal s;
1620+
std::reference_wrapper<foo::StreamableInLocal> r(s);
1621+
EXPECT_STREQ("StreamableInLocal", PrintToString(r).c_str());
1622+
}
1623+
1624+
TEST(PrintToStringTest, PrintReferenceToStreamableInGlobal) {
1625+
StreamableInGlobal s;
1626+
std::reference_wrapper<StreamableInGlobal> r(s);
1627+
EXPECT_STREQ("StreamableInGlobal", PrintToString(r).c_str());
1628+
}
1629+
16071630
TEST(IsValidUTF8Test, IllFormedUTF8) {
16081631
// The following test strings are ill-formed UTF-8 and are printed
16091632
// as hex only (or ASCII, in case of ASCII bytes) because IsValidUTF8() is

0 commit comments

Comments
 (0)