From e61a86bbb25f9e0943387738f1e138743c8fe166 Mon Sep 17 00:00:00 2001 From: Gleb Natapov Date: Thu, 29 Aug 2019 13:09:02 +0300 Subject: [PATCH] to_string: Add operator<< overload for std::tuple. Message-Id: <20190829100902.GN21540@scylladb.com> --- to_string.hh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/to_string.hh b/to_string.hh index 6295144827..a17ef63cac 100644 --- a/to_string.hh +++ b/to_string.hh @@ -51,6 +51,20 @@ sstring join(sstring delimiter, const PrintableRange& items) { return join(delimiter, items.begin(), items.end()); } +template +struct print_with_comma { + const Printable& v; +}; + +template +std::ostream& operator<<(std::ostream& os, const print_with_comma& x) { + os << x.v; + if (NeedsComma) { + os << ", "; + } + return os; +} + namespace std { template @@ -87,6 +101,16 @@ std::ostream& operator<<(std::ostream& os, const std::pair& p) { return os; } +template +std::ostream& print_tuple(std::ostream& os, const std::tuple& p, std::index_sequence) { + return ((os << "{" ) << ... << print_with_comma{std::get(p)}) << "}"; +} + +template +std::ostream& operator<<(std::ostream& os, const std::tuple& p) { + return print_tuple(os, p, std::make_index_sequence()); +} + template std::ostream& operator<<(std::ostream& os, const std::unordered_set& items) { os << "{" << join(", ", items) << "}";