From 421ae75fa94a05b71c03255af4ad597a60fc8ed9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 8 Mar 2024 01:17:11 +0000 Subject: Rework stream support to work with any collection --- lib/stream_support.h | 16 ++++------------ test/test-static-stream_support.cpp | 21 +++++++++++++++------ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/stream_support.h b/lib/stream_support.h index fa536f1..57d82a1 100644 --- a/lib/stream_support.h +++ b/lib/stream_support.h @@ -11,17 +11,16 @@ template concept stringlike = requires(const S & s) { s.substr(0); }; template -concept spanable = std::is_constructible_v, T> && !stringlike - && !std::is_same_v, T>; +concept NonStringIterableCollection + = std::is_same_v().begin()), decltype(std::declval().end())> && !stringlike; namespace std { - template std::ostream & - operator<<(std::ostream & s, const span v) + operator<<(std::ostream & s, const NonStringIterableCollection auto & v) { s << '('; for (const auto & i : v) { - if (&i != &v.front()) { + if (&i != &*v.begin()) { s << ", "; } s << i; @@ -43,13 +42,6 @@ namespace std { return (s << std::span {&v[0], L}); } - template - std::ostream & - operator<<(std::ostream & s, const T & v) - { - return (s << std::span {v}); - } - template std::ostream & operator<<(std::ostream & s, const std::pair & v) diff --git a/test/test-static-stream_support.cpp b/test/test-static-stream_support.cpp index 3002ccc..6bf9ea4 100644 --- a/test/test-static-stream_support.cpp +++ b/test/test-static-stream_support.cpp @@ -1,11 +1,20 @@ #include "stream_support.h" #include +#include +#include #include -static_assert(spanable>); -static_assert(spanable>); -static_assert(spanable>); -static_assert(spanable>); -static_assert(!spanable); -static_assert(!spanable); +static_assert(NonStringIterableCollection>); +static_assert(NonStringIterableCollection>); +static_assert(NonStringIterableCollection>); +static_assert(NonStringIterableCollection>); +static_assert(NonStringIterableCollection>); +static_assert(NonStringIterableCollection>); +static_assert(!NonStringIterableCollection); +static_assert(!NonStringIterableCollection); + +static_assert(requires(std::vector i, std::ostream & o) { o << i; }); +static_assert(requires(std::array i, std::ostream & o) { o << i; }); +static_assert(requires(std::set i, std::ostream & o) { o << i; }); +static_assert(requires(std::map i, std::ostream & o) { o << i; }); -- cgit v1.2.3