Skip to content

Commit 5c01794

Browse files
Abseil Teamcopybara-github
authored andcommitted
Support passing I/O manipulators such as std::endl to StringifyStream
PiperOrigin-RevId: 892740803 Change-Id: I7c0c2d330080492d1451960bc21c04e6dbcbeb22
1 parent 170f877 commit 5c01794

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

absl/strings/internal/stringify_stream.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ class StringifyStream {
8282
return stream << t;
8383
}
8484

85+
// Overload for things like << std::endl which need an explicit type in order
86+
// to resolve to the appropriate overload or template instantiation.
87+
StringifyStream& operator<<(std::ostream& (*func)(std::ostream&)) {
88+
sink_.os << func;
89+
return *this;
90+
}
91+
8592
private:
8693
// Abseil "stringify sink" concept (stringify_sink.h)
8794
struct Sink {

absl/strings/internal/stringify_stream_test.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "absl/strings/internal/stringify_stream.h"
1616

1717
#include <cstddef>
18+
#include <iomanip>
1819
#include <ostream>
1920
#include <sstream>
2021

@@ -93,6 +94,16 @@ TEST(StringifyStreamTest, PreferStreamInsertionOverAbslStringify) {
9394
StringifyStream(os) << PreferStreamInsertionOverAbslStringifyTest{};
9495
EXPECT_EQ(os.str(), "good");
9596
}
97+
TEST(StringifyStreamTest, SupportEndl) {
98+
std::ostringstream os;
99+
StringifyStream(os) << std::endl;
100+
EXPECT_EQ(os.str(), "\n");
101+
}
102+
TEST(StringifyStreamTest, SupportSetbase) {
103+
std::ostringstream os;
104+
StringifyStream(os) << std::setbase(16) << 255;
105+
EXPECT_EQ(os.str(), "ff");
106+
}
96107

97108
} // namespace
98109
} // namespace strings_internal

0 commit comments

Comments
 (0)