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

A Crossword for CppCon 2024

12 September, 202411 September, 2024

As well as being a C++ enthusiast, I’m a bit of a cruciverbalist. I do the Guardian cryptic every day (not in a particularly competitive time — I’d count sub-20 minutes as a good day), and sometimes others when I can. Like many Brits, I learned to do cryptic crosswords…

Read More

Ellipses & Splines again

10 March, 2008

After some code cleanup and generalisation, I can now modulate whole splines onto ellipses and onto splines themselves. Here is my simple 4-bezier spline modulated onto an ellipse: And onto itself: Repeatedly modulating a spline onto itself while varying the frequency parameter leads to some interesting and fractal patterns. Nice.

Read More

C++23’s new function syntax

5 September, 20225 September, 2022

We’ve had a couple of ways to spell functions for a long time: And we’ve had a shortcut for that second one for a while, too: (Aside: notice where [[nodiscard]] is positioned, since C++23’s P2173, to apply to the lambda’s call operator.) All these ways to spell functions look the…

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