namespace notstd {
template<std::size_t...Is>
struct index_sequence {};
template<std::size_t N, std::size_t...Is>
struct make_index_sequence:make_index_sequence<N-1,N-1,Is...>{};
template<std::size_t...Is>
struct make_index_sequence<0,Is...>:index_sequence<Is...>{};
#define RETURNS(...) \
noexcept(noexcept(__VA_ARGS__)) \
-> decltype(__VA_ARGS__) \
{ return __VA_ARGS__; }
namespace details {
template<class F, class Tuple, std::size_t...Is>
auto apply( F&& f, Tuple&& tuple, index_sequence<Is...> )
RETURNS( std::forward<F>(f)( std::get<Is>(std::forward<Tuple>(tuple))... ) )
template<class Tuple>
using raw_tuple = typename std::remove_cv<typename std::remove_reference<Tuple>::type>::type;
template<class Tuple>
using tuple_count = std::tuple_size< raw_tuple<Tuple> >;
}
template<class F, class Tuple>
auto apply( F&& f, Tuple&& tuple )
RETURNS(
::notstd::details::apply(
std::forward<F>(f),
std::forward<Tuple>(tuple),
::notstd::make_index_sequence<
::notstd::details::tuple_count<Tuple>::value
>{}
)
)
}
now this ::notstd::apply
behaves a lot like C++17's std::apply
.
We then glue this to your ToString
via ToStream
:
struct to_stream_t;
template<class...Args>
void ToStream(std::ostream& os, const std::tuple<Args...>& t) {
os << '{';
::notstd::apply( to_stream_t{os}, t );
os << '}';
}
inline void ToStream(std::ostream&) {}
template<class T>
void ToStream(std::ostream& os, const T& t) {
os << t;
}
template<class T0, class... Ts>
void ToStream(std::ostream& os, const T0& t0, const Ts& ... ts) {
ToStream(os, t0);
using discard=int[];
(void)discard{0,((
void(os << ' '), to_stream_t{os}(ts)
),0)...};
}
struct to_stream_t {
std::ostream& os;
template<class...Args>
void operator()(Args const&...args) const {
ToStream(os, args...);
}
};
template<class...Ts>
std::string ToString( Ts const&... ts ) {
std::stringstream ss;
ToStream( ss, ts... );
return ss.str();
}
this also flattens recursive tuples.
If you add more std
or fundamental type manual ToStream
implementations, put them before the body of to_stream_t
or recursion won't work. And recurse via to_stream_t{os}(t)
instead of ToStream(os, t)
generally so you find the right overloads.
Test code:
std::tuple<std::string, std::string, int> t("hello", "world", 42);
std::cout << ToString(t, "-", t);
Live example.
We can augment with vector support:
template<class T, class A>
void ToStream(std::ostream& os, const std::vector<T, A>& v) {
os << '[';
for (auto const& x:v)
{
if (std::addressof(x) != v.data())
os << ',';
to_stream_t{os}(x);
}
os << ']';
}
then test with all of this:
std::tuple<std::string, std::string, int> t("hello", "world", 42);
std::cout << ToString(t, "-", t) << "\n";
std::vector< int > v {1,2,3};
std::cout << ToString(v) << "\n";
std::vector< std::tuple<int, int> > v2 {{1,2},{3,4}};
std::cout << ToString(v2) << "\n";
auto t2 = std::tie( v, v2 );
std::cout << ToString(t2) << "\n";
Live example.
The end output is:
{hello world 42} - {hello world 42}
[1,2,3]
[{1 2},{3 4}]
{[1,2,3] [{1 2},{3 4}]}
as expected.