Created
February 13, 2016 11:34
-
-
Save studiofuga/3539414bf0ab27a8cf73 to your computer and use it in GitHub Desktop.
Pretty print a std::tuple to a stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef STREAMTUPLE_H | |
#define STREAMTUPLE_H | |
#include <iostream> | |
#include <tuple> | |
namespace aux{ | |
template<std::size_t...> struct seq{}; | |
template<std::size_t N, std::size_t... Is> | |
struct gen_seq : gen_seq<N-1, N-1, Is...>{}; | |
template<std::size_t... Is> | |
struct gen_seq<0, Is...> : seq<Is...>{}; | |
template<class Ch, class Tr, class Tuple, std::size_t... Is> | |
void print_tuple(std::basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){ | |
using swallow = int[]; | |
(void)swallow{0, (void(os << (Is == 0? "" : ", ") << std::get<Is>(t)), 0)...}; | |
} | |
} // aux:: | |
template<class Ch, class Tr, class... Args> | |
auto operator<<(std::basic_ostream<Ch, Tr>& os, std::tuple<Args...> const& t) | |
-> std::basic_ostream<Ch, Tr>& | |
{ | |
os << "("; | |
aux::print_tuple(os, t, aux::gen_seq<sizeof...(Args)>()); | |
return os << ")"; | |
} | |
#endif // STREAMTUPLE_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment