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 5)

elbeno, 2 February, 201530 June, 2015

Part 1 Part 2 Part 3 Part 4 Part 5 Postscript

So far, we can print containers, but what about arrays? And what about “pretty-printing” strings – perhaps we need to wrap them with quotes. Well, we know that with the existing code, both arrays and strings count as outputtable. Both std::string and char* (and its variously const friends) can be passed to operator<<, and so can arrays, because they decay to pointers.

So, we should be able to deal with this using common-or-garden specialization on stringifier_select. Specializing for std::string is easy:

template 
struct stringifier_select, 
                          is_outputtable_tag>
{
  using S = std::basic_string;
  explicit stringifier_select(const S& t) : m_t(t) {}

  std::ostream& output(std::ostream& s) const
  {
    return s << C('\"') << m_t << C('\"');
  }

  const S& m_t;
};

And specializing for the other char* related types is also as easy, albeit verbose because of dealing with char*, const char*, char* const and const char* const.

Specializing for arrays of char is just as easy. This time we have just the array size as a template argument. And once again, there is a specialization for arrays of const char that is identical.

template 
struct stringifier_select
{
  using S = char[N];
  explicit stringifier_select(const S& t) : m_t(t) {}

  std::ostream& output(std::ostream& s) const
  {
    return s << '\"' << m_t << '\"';
  }

  const S& m_t;
};

Specializing for arrays (other than of char) is also easy, and gives us a chance to abstract out the code that we used for the iterable printing. By happy circumstance (i.e. by design!), arrays support std::begin() and std::end(), so we can write the following:

template 
std::ostream& output_iterable(std::ostream& s, const T& t)
{
  s << iterable_opener()(t);
  auto b = std::begin(t);
  auto e = std::end(t);
  if (b != e)
    s << prettyprint(*b);
  std::for_each(++b, e,
                [&s, &t] (auto& e)
                { s << iterable_separator()(t)
                    << prettyprint(e); });
  return s << iterable_closer()(t);
}

template 
struct stringifier_select
{
  using S = T[N];
  explicit stringifier_select(const S& t) : m_t(t) {}

  std::ostream& output(std::ostream& s) const
  {
    return output_iterable(s, m_t);
  }

  const S& m_t;
};

And the code for printing iterable things changes likewise. Unlike the situation with char*, we don't need to deal with const and non-const separately because here, T itself is inferred to be const or not.

And that's pretty much it - just a couple more things to add. I mentioned enum classes back in part 1, and here's how we print out their values:

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

  std::ostream& output(std::ostream& s) const
  {
    return s << static_cast>(m_t);
  }

  T m_t;
};

Simple. Two final things to add: first, specialize for pretty-printing bool equivalently to using std::boolalpha; second, distinguish a few "unprintable" things and output something for them - classes without operator<<, unions, nullptr. The code that does this is very similar to what we've already seen.

So now, we can pretty-print all "normally" printable things, containers, pairs and tuples, callable things, certain unprintables that we can meaningfully label, and really unprintable things with a fallback. I think that'll do for the time being. It's been a fun journey, exploring TMP techniques, C++ type support, mapping over tuples, and the amazing void_t.

You can find all the resulting code from this exercise on github.

C++ Programming

Post navigation

Previous post
Next post

Related Posts

A Crossword for CppCon 2025 – Solutions

22 September, 202522 September, 2025

See https://www.elbeno.com/blog/?p=1795 (or https://crosshare.org/crosswords/aZ0lpCTDSztWEsvvuboa/cppcon-2025) if you want to try it. If you’re here for solutions, read on. Definitions are given in italics. My personal likes are 4D and 13D. 1A BABBAGE Supergroup among leaders to back grant endowment for computing pioneer (7) ABBA (“supergroup”) inserted into (“among”) BGE (“leaders to…

Read More

C++ Reflection: Another Monad

4 March, 20264 March, 2026

“Discovery consists of seeing what everybody has seen, and thinking what nobody has thought.” — Albert Szent-Györgi, 1937 Nobel Laureate for Medicine I’m sure others have thought this, but they’re certainly not saying much about it. Barry’s recent blog post “Behold the power of meta::substitute” got so close to saying…

Read More

Another myth, about C++ lambdas

16 March, 201530 June, 2015

Myth: Lambda expressions can cause heap allocations. I see this myth coming up a lot. People think that lambdas can cause a heap allocation – on Reddit, on StackOverflow, on Channel9 comments, in personal conversations. I don’t blame people for thinking this – C++ is complex. But it always seems…

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