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++ (postscript)

elbeno, 2 February, 201530 June, 2015

Part 1 Part 2 Part 3 Part 4 Part 5 Postscript

Refactoring! My initial plan for customizing opener/closer/separator for containers turned out to be unwieldy: I realized that it wouldn’t be possible for me to provide default specializations and also allow clients to specialize. Also, you may have noticed that the code for printing pairs, tuples, strings and arrays didn’t allow for customization at all. So I reworked the customization, allowing a formatter object as a parameter to prettyprint() and providing a default one:

template 
inline stringifier, default_formatter>
prettyprint(T&& t)
{
  return stringifier, default_formatter>(
      std::forward(t), default_formatter());
}

template 
inline stringifier, F>
prettyprint(T&& t, F&& f)
{
  return stringifier, F>(
      std::forward(t), std::forward(f));
}

The default_formatter looks like this:

struct default_formatter
{
  // default separator, opener and closer
  template 
  constexpr const char* separator(const T&) const
  { return ","; }

  template 
  constexpr const char* opener(const T&) const
  { return "{"; }

  template 
  constexpr const char* closer(const T&) const
  { return "}"; }

  // use [] for vectors
  template 
  constexpr const char* opener(const std::vector&) const
  { return "["; }

  template 
  constexpr const char* closer(const std::vector&) const
  { return "]"; }

  // etc (more omitted)
  ...
};

And now I can pass the formatter object through to each specialization of stringifier_select, and use it appropriately for pairs, tuples, strings and arrays, as well as iterables. When I want to override the formatter, I simply specify a new formatter type, implement the opener/closer/separator functions for the type in question the way I want to, and pass an instance to prettyprint.

C++ Programming

Post navigation

Previous post
Next post

Related Posts

Functions are Asymmetric

10 October, 202510 October, 2025

Here are some functions: What they all have in common, structurally, is that they each return one thing. Even though they may take any number of arguments, they must each have one return value. One shall be the number, and the number shall be one. This is true of all…

Read More

Functional Rainbow

29 December, 200829 December, 2008
Read More

Thoughts on Modern C++ and Game Dev

1 January, 20191 January, 2019

TL;DR: The C++ committee isn’t following some sort of agenda to ignore the needs of game programmers, and “modern” C++ isn’t going to become undebuggable. — Over the past week there has been an ongoing conversation on Twitter about how many people — especially those in the games industry —…

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