This article is a continuation of a previous discussion on pretty-printing STL containers, where we developed an elegant and general solution for this task.
Problem Statement
In this extension, we aim to include pretty-printing functionality for std::tuple objects using variadic templates, specifically tailored for C 11. Inspired by the previous example for std::pair, we seek an analogous construction for tuples. The desired behavior is as follows:
auto a = std::make_tuple(5, "Hello", -0.1); std::cout << a << std::endl; // prints: (5, "Hello", -0.1)
Additionally, we aim to incorporate the same level of generality as the previous question, including support for different character types and customizable pair delimiters.
Solution
Exploiting the indices-based approach, we can construct a solution using the following code snippet:
namespace aux{ template<std::size_t... Is> 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)...}; } } 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 << ")"; }
For the customization of delimiters, we can add these partial specializations:
// Delimiters for tuple template<class... Args> struct delimiters<std::tuple<Args...>, char> { static const delimiters_values<char> values; }; template<class... Args> const delimiters_values<char> delimiters<std::tuple<Args...>, char>::values = { "(", ", ", ")" }; template<class... Args> struct delimiters<std::tuple<Args...>, wchar_t> { static const delimiters_values<wchar_t> values; }; template<class... Args> const delimiters_values<wchar_t> delimiters< std::tuple<Args...>, wchar_t >::values = { L"(", L", ", L")" };
With these modifications, the operator<< and print_tuple functions can be updated to incorporate the delimiter customization:
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[]; char const* delim = delimiters<Tuple, Ch>::values.delimiter; if(!delim) delim = ""; (void)swallow{0, (void( os << (Is == 0? "" : delim) << std::get<Is>(t) ), 0)...}; }
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>& { typedef std::tuple<Args...> tuple_t; if(delimiters<tuple_t, Ch>::values.prefix != 0) os << delimiters<tuple_t,char>::values.prefix; print_tuple(os, t, aux::gen_seq<sizeof...(Args)>() ); if(delimiters<tuple_t, Ch>::values.postfix != 0) os << delimiters<tuple_t,char>::values.postfix; return os; }
The above is the detailed content of How to Pretty-Print `std::tuple` in C 11 Using Variadic Templates?. For more information, please follow other related articles on the PHP Chinese website!