From 8dfe3d339dc1542ee7bd59c000302765648e972c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 29 Aug 2022 12:54:18 +0100 Subject: Generic solution for streaming collections that can be spanned --- lib/stream_support.cpp | 11 +++++++++++ lib/stream_support.hpp | 18 ++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 lib/stream_support.cpp diff --git a/lib/stream_support.cpp b/lib/stream_support.cpp new file mode 100644 index 0000000..2054873 --- /dev/null +++ b/lib/stream_support.cpp @@ -0,0 +1,11 @@ +#include "stream_support.hpp" + +#include +#include + +static_assert(spanable>); +// static_assert(spanable>); +static_assert(spanable>); +// static_assert(spanable>); +static_assert(!spanable); +static_assert(!spanable); diff --git a/lib/stream_support.hpp b/lib/stream_support.hpp index 0552b9d..3f0165c 100644 --- a/lib/stream_support.hpp +++ b/lib/stream_support.hpp @@ -5,6 +5,11 @@ #include #include #include +#include + +template +concept spanable = std::is_constructible_v, T> && !std::is_same_v>; namespace std { template @@ -34,16 +39,9 @@ namespace std { return (s << std::span {&v[0], L}); } - template - std::ostream & - operator<<(std::ostream & s, const array & v) - { - return (s << std::span {v}); - } - - template + template std::ostream & - operator<<(std::ostream & s, const vector & v) + operator<<(std::ostream & s, const T & v) { return (s << std::span {v}); } @@ -61,7 +59,7 @@ streamed_string(const T & v) { std::stringstream ss; ss << v; - return ss.str(); + return std::move(ss).str(); } #define CLOG(x) std::cerr << #x " : " << x << "\n"; -- cgit v1.2.3