Home >Backend Development >C++ >How to Pretty-Print `std::tuple` in C 11 Using Variadic Templates?

How to Pretty-Print `std::tuple` in C 11 Using Variadic Templates?

Barbara Streisand
Barbara StreisandOriginal
2024-11-07 17:44:03924browse

How to Pretty-Print `std::tuple` in C  11 Using Variadic Templates?

Pretty-printing std::tuple

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 <p>Additionally, we aim to incorporate the same level of generality as the previous question, including support for different character types and customizable pair delimiters.</p><p><strong>Solution</strong></p><p>Exploiting the indices-based approach, we can construct a solution using the following code snippet:</p><pre class="brush:php;toolbar:false">namespace aux{
template<:size_t... is> struct seq{};

template<:size_t n std::size_t... is>
struct gen_seq : gen_seq<n-1 n-1 is...>{};

template<:size_t... is>
struct gen_seq : seq<is...>{};

template<class ch class tr tuple std::size_t... is>
void print_tuple(std::basic_ostream<ch>& os, Tuple const& t, seq<is...>)
{
  using swallow = int[];
  (void)swallow{0, (void(
    os (t)
  ), 0)...};
}
}

template<class ch class tr class... args>
auto operator& os, std::tuple<args...> const& t)
    -> std::basic_ostream<ch tr>&
{
  os ()
);
  return os <p>For the customization of delimiters, we can add these partial specializations:</p>
<pre class="brush:php;toolbar:false">// Delimiters for tuple
template<class... args>
struct delimiters<:tuple>, char> {
  static const delimiters_values<char> values;
};

template<class... args>
const delimiters_values<char> delimiters<:tuple>, char>::values =
    { "(", ", ", ")" };

template<class... args>
struct delimiters<:tuple>, wchar_t> {
  static const delimiters_values<wchar_t> values;
};

template<class... args>
const delimiters_values<wchar_t> delimiters, wchar_t
>::values = { L"(", L", ", L")" };</wchar_t></class...></wchar_t></:tuple></class...></:tuple></char></class...></char></:tuple></class...>

With these modifications, the operator

template<class ch class tr 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 (t)
  ), 0)...};
}</tuple></is...></ch></class>
template<class ch class tr class... args>
auto operator& 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 ::values.prefix;

  print_tuple(os, t, aux::gen_seq<sizeof...>()
);

  if(delimiters<tuple_t ch>::values.postfix != 0)
    os ::values.postfix;

  return os;
}</tuple_t></sizeof...></tuple_t></args...></ch></args...></class>

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn