首页  >  文章  >  后端开发  >  如何使用可变参数模板和自定义分隔符漂亮地打印'std::tuple”?

如何使用可变参数模板和自定义分隔符漂亮地打印'std::tuple”?

Susan Sarandon
Susan Sarandon原创
2024-11-08 07:27:011054浏览

How can you pretty-print `std::tuple` using variadic templates and custom delimiters?

漂亮打印 std::tuple

在上一个问题中,我们为漂亮打印 STL 容器创建了一个优雅的解决方案。现在,让我们解决 std::tuple 的漂亮打印问题使用可变参数模板。

对于 std::pair,我们可以使用自定义运算符进行打印:

std::ostream &operator<<(std::ostream &o, const std::pair<S,T> &p)
{
  return o << "(" << p.first << ", " << p.second << ")";
}

要将其推广到元组,我们可以利用模板参数解包和索引生成:

namespace aux{
template<std::size_t... Is> struct seq{}; // dummy helper type

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...>){ // overload to print tuples
  using swallow = int[]; // silence unused warning
  (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)>()); // call template overload with index helper to print
  return os << ")";
}

添加自定义分隔符(例如,对于 char 和wchar_t),我们可以使用部分特化:

// 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")" };

并调整运算符

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;
}

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...>){ 
  char const* delim = delimiters<Tuple, Ch>::values.delimiter;
  if(!delim) delim = "";
  (void)swallow{0, (void(os << (Is == 0? "" : delim) << std::get<Is>(t)), 0)...};
}

现在,使用自定义分隔符打印 std::tuple 非常简单:

auto a = std::make_tuple(5, "Hello", -0.1);
std::cout << a << std::endl; // prints: (5, "Hello", -0.1)

以上是如何使用可变参数模板和自定义分隔符漂亮地打印'std::tuple”?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn