Skip to content
Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

How to print anything in C++ (part 3)

elbeno, 2 February, 201530 June, 2015

Part 1 Part 2 Part 3 Part 4 Part 5 Postscript

So far, we’ve dealt with things that are already outputtable with operator<<, and things that are iterable with begin() and end(). To round out the “containers”, we need to deal with pair and tuple. It’s simple to print a pair:

template 
struct stringifier_select
{
  explicit stringifier_select(const T& t) : m_t(t) {}

  std::ostream& output(std::ostream& s) const
  {
    return s << '(' << prettyprint(m_t.first)
             << ',' << prettyprint(m_t.second) << ')';
  }

  const T& m_t;
};

Printing a tuple is a little harder. There is no easy way to iterate through a tuple, but that's what we want to do. A tuple is designed for compile-time member access where you know either the index (from C++11) or the type (from C++14) of the element you want. So what we need is a compile-time way of iterating through an index. And that's what std::index_sequence is for.

template 
inline void for_each_in_tuple(const std::tuple& t,
                              F f,
                              std::index_sequence)
{
  (void)(int[]) { 0, (f(std::get(t), Is), 0)... };
}
template 
inline void for_each_in_tuple(const std::tuple& t, F f)
{
  for_each_in_tuple(t, f, std::make_index_sequence());
}

This is a handy function to have around. It applies a function to each element of a tuple. First (in the second function seen here), we create a std::index_sequence equal to the size of the tuple. Then (in the top function) we construct an int array using uniform initialization. To deal with zero-length tuples, the array has one zero element to begin. After that, each element of the array is our function applied to the thing at the current index and the index itself, sequenced with zero using the comma operator, so that whatever is returned, the array gets a zero and is happy.

I decided to pass both the element and the index to the function to deal with the separator fencepost problem (as with containers). So outputting a tuple looks like this:

template 
struct stringifier_select
{
  explicit stringifier_select(const T& t) : m_t(t) {}

  std::ostream& output(std::ostream& s) const
  {
    s << '(';
    for_each_in_tuple(m_t,
                      [&s] (auto& e, size_t i)
                      { if (i > 0) s << ',';
                        s << prettyprint(e); });
    return s << ')';
  }

  const T& m_t;
};

Going well so far. Next, I'll look at distinguishing callable things.

C++ Programming

Post navigation

Previous post
Next post

Related Posts

A Crossword for CppCon 2025

14 September, 202522 September, 2025

Another year, another crossword. With some of the clues/answers vaguely themed. You can solve it online or offline with the PDF. Answers after the con.

Read More

How a Bug Made Me a Better Programmer

1 March, 20091 March, 2009

This is the tale of a bug. A challenging bug I encountered some time ago, and which changed my career for the better. It was late 2004. I was lead multiplayer engineer on Goldeneye: Rogue Agent, a title which could charitably be described as mediocre, but I like to think…

Read More

Doxygen and C++: <i>not</i> a marriage made in heaven

19 August, 200529 July, 2007

I briefly interrupt your scheduled browsing… Doxygen is a documentation system for C++, C, Java, Objective-C, Python, IDL (Corba and Microsoft flavors) and to some extent PHP, C#, and D. So it says on the website, and so it is. It’s also a Good Thing, most people would assume. Doxygen…

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

©2026 Why is a raven like a writing desk? | WordPress Theme by SuperbThemes