[llvm] d5acc83 - Implement LWG#1203 for raw_ostream.
Artem Belevich via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 9 14:01:30 PST 2019
Author: Christian Sigg
Date: 2019-12-09T14:00:53-08:00
New Revision: d5acc83a3ac3db85ca30b9e73b1bdb112f313d1b
URL: https://github.com/llvm/llvm-project/commit/d5acc83a3ac3db85ca30b9e73b1bdb112f313d1b
DIFF: https://github.com/llvm/llvm-project/commit/d5acc83a3ac3db85ca30b9e73b1bdb112f313d1b.diff
LOG: Implement LWG#1203 for raw_ostream.
Implement LWG#1203 (https://cplusplus.github.io/LWG/issue1203) for raw_ostream
like libc++ does for std::basic_ostream<...>.
Add a operator<< overload that takes an rvalue reference of a typed derived from
raw_ostream, streams the value to it and returns the stream of the same type as
the argument.
This allows free operator<< to work with rvalue reference raw_ostreams:
raw_ostream& operator<<(raw_ostream&, const SomeType& Value);
raw_os_ostream(std::cout) << SomeType();
It also allows using the derived type like:
auto Foo = (raw_string_ostream(buffer) << "foo").str();
Author: Christian Sigg <csigg at google.com>
Differential Revision: https://reviews.llvm.org/D70686
Added:
Modified:
llvm/include/llvm/Support/raw_ostream.h
llvm/unittests/Support/raw_ostream_test.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index 705f1790b1d3..c8770c337588 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -21,6 +21,7 @@
#include <cstring>
#include <string>
#include <system_error>
+#include <type_traits>
namespace llvm {
@@ -354,6 +355,17 @@ class raw_ostream {
virtual void anchor();
};
+/// Call the appropriate insertion operator, given an rvalue reference to a
+/// raw_ostream object and return a stream of the same type as the argument.
+template <typename OStream, typename T>
+typename std::enable_if<!std::is_reference<OStream>::value &&
+ std::is_base_of<raw_ostream, OStream>::value,
+ OStream &&>::type
+operator<<(OStream &&OS, const T &Value) {
+ OS << Value;
+ return std::move(OS);
+}
+
/// An abstract base class for streams implementations that also support a
/// pwrite operation. This is useful for code that can mostly stream out data,
/// but needs to patch in a header that needs to know the output size.
diff --git a/llvm/unittests/Support/raw_ostream_test.cpp b/llvm/unittests/Support/raw_ostream_test.cpp
index 497d31317ce7..0e56464e5730 100644
--- a/llvm/unittests/Support/raw_ostream_test.cpp
+++ b/llvm/unittests/Support/raw_ostream_test.cpp
@@ -18,8 +18,7 @@ namespace {
template<typename T> std::string printToString(const T &Value) {
std::string res;
- llvm::raw_string_ostream(res) << Value;
- return res;
+ return (llvm::raw_string_ostream(res) << Value).str();
}
/// printToString - Print the given value to a stream which only has \arg
@@ -47,6 +46,10 @@ template<typename T> std::string printToStringUnbuffered(const T &Value) {
return res;
}
+struct X {};
+
+raw_ostream &operator<<(raw_ostream &OS, const X &) { return OS << 'X'; }
+
TEST(raw_ostreamTest, Types_Buffered) {
// Char
EXPECT_EQ("c", printToString('c'));
@@ -76,6 +79,9 @@ TEST(raw_ostreamTest, Types_Buffered) {
// Min and max.
EXPECT_EQ("18446744073709551615", printToString(UINT64_MAX));
EXPECT_EQ("-9223372036854775808", printToString(INT64_MIN));
+
+ // X, checking free operator<<().
+ EXPECT_EQ("X", printToString(X{}));
}
TEST(raw_ostreamTest, Types_Unbuffered) {
@@ -107,6 +113,9 @@ TEST(raw_ostreamTest, Types_Unbuffered) {
// Min and max.
EXPECT_EQ("18446744073709551615", printToStringUnbuffered(UINT64_MAX));
EXPECT_EQ("-9223372036854775808", printToStringUnbuffered(INT64_MIN));
+
+ // X, checking free operator<<().
+ EXPECT_EQ("X", printToString(X{}));
}
TEST(raw_ostreamTest, BufferEdge) {
More information about the llvm-commits
mailing list