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

An algorithmic sketch: inplace_merge

13 March, 201618 March, 2016

One of the things I like to do in my spare time is study the STL algorithms. It is easy to take them for granted and easy, perhaps, to imagine that they are mostly trivial. And some are: I would think that any decent interview candidate ought to be able…

Read More

Why Reading Books Matters to a Programmer

29 April, 200829 April, 2008

The programming book market these days is small. Nothing like what it was eight years ago or so. And apparently, programmers don’t read books (any more). It’s mostly true. But of course, there are still books worth reading. I’m going to take as read the easy arguments: let’s assume that…

Read More

Thoughts on Default Construction

16 August, 2017

What does default construction mean? Why do we write default constructors? When and why should we require them? I’ve been pondering these questions lately. One of the great things that C++ gets right is that it grants programmers the ability to create types that behave like built-in types. Many languages…

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