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

Experimenting with (cubic) Bézier subdivision

27 February, 20081 March, 2008

As you know, a Bézier curve (here meaning specifically a cubic Bézier) is often used for drawing all kinds of things in vector graphics. It has the nice property that the endpoints and control points form a bounding box, and deCasteljau’s algorithm is a nice numerically stable way of evaluating…

Read More

Curve and Vector

30 March, 200830 March, 2008

Curve (com.elbeno.curve) is my common lisp package for doing cool things with two-dimensional curves. In particular, modulating cubic Bézier curves and splines, but also approximating arbitrary elliptical and circular arc segments with cubic Bézier curves. It depends on Vector (com.elbeno.vector), a cobbled together set of functionality for representing and manipulating…

Read More

a maths day

15 June, 200629 July, 2007

Since is always posting about his antics bending Maya to his will, I felt like sharing my working day. Without going into detail about what I’m working on (which is still hush-hush until it’s announced), I can say that today I did more maths than I’ve done in a long…

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