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
This commit is contained in:
Abseil Team 2022-10-14 10:32:14 -07:00 committed by Copybara-Service
parent 67174c7675
commit 137b6e2770
2 changed files with 30 additions and 0 deletions

View File

@ -891,6 +891,13 @@ class UniversalTersePrinter<T&> {
UniversalPrint(value, os);
}
};
template <typename T>
class UniversalTersePrinter<std::reference_wrapper<T>> {
public:
static void Print(std::reference_wrapper<T> value, ::std::ostream* os) {
UniversalTersePrinter<T>::Print(value.get(), os);
}
};
template <typename T, size_t N>
class UniversalTersePrinter<T[N]> {
public:

View File

@ -37,6 +37,7 @@
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <limits>
#include <list>
#include <map>
@ -193,6 +194,11 @@ OutputStream& operator<<(OutputStream& os,
return os;
}
struct StreamableInLocal {};
void operator<<(::std::ostream& os, const StreamableInLocal& /* x */) {
os << "StreamableInLocal";
}
// A user-defined streamable but recursively-defined container type in
// a user namespace, it mimics therefore std::filesystem::path or
// boost::filesystem::path.
@ -1604,6 +1610,23 @@ TEST(PrintToStringTest, ContainsNonLatin) {
"\n As Text: \"From ä — ẑ\"");
}
TEST(PrintToStringTest, PrintStreamableInLocal) {
EXPECT_STREQ("StreamableInLocal",
PrintToString(foo::StreamableInLocal()).c_str());
}
TEST(PrintToStringTest, PrintReferenceToStreamableInLocal) {
foo::StreamableInLocal s;
std::reference_wrapper<foo::StreamableInLocal> r(s);
EXPECT_STREQ("StreamableInLocal", PrintToString(r).c_str());
}
TEST(PrintToStringTest, PrintReferenceToStreamableInGlobal) {
StreamableInGlobal s;
std::reference_wrapper<StreamableInGlobal> r(s);
EXPECT_STREQ("StreamableInGlobal", PrintToString(r).c_str());
}
TEST(IsValidUTF8Test, IllFormedUTF8) {
// The following test strings are ill-formed UTF-8 and are printed
// as hex only (or ASCII, in case of ASCII bytes) because IsValidUTF8() is