User-Defined Formatting in std::format – Part 2 -- Spencer Collyer
Last time, we saw how to provide formatting for a simple user-defined class. Spencer Collyer builds on this, showing how to write a formatter for more complicated types.
User-Defined Formatting in std::format – Part 2
by Spencer Collyer
From the article:
In the previous article in this series [Collyer24], I showed how to write a class to format user-defined classes using the
std::formatlibrary. In this article I will describe how this can be extended to container classes or any other class that holds objects whose type is specified by the user of your class.A note on the code listings: The code listings in this article have lines labelled with comments like
// 1. Where these lines are referred to in the text of this article, it will be as ‘line1’ for instance, rather than ‘the line labelled// 1’.Nested formatter objects
The objects created from the
formattertemplate structs are just ordinary C++ objects – there is nothing special about them 1. In particular, there is nothing to stop you including an object of aformattertemplate type inside one of your user-definedformatterstructs.You might wonder why you would want to do that. One simple case is if you have a templated container class, and want to create a
formatterthat can output the container in one go, rather than having to write code to iterate over the container and output each value in turn. Having a nestedformatterfor the contained value type allows you to do this and allow the values to be formatted differently to the default, as the following examples will show. Other uses will no doubt come to mind for your own classes.

The conclusion of the last post was that we need to change something in our models: maybe std::vector should use a different strategy when erasing elements; maybe types like std::tuple<int &> should not be allowed to be stored in a vector; maybe Qt should not be using memmove when erasing objects of trivially relocatable type (but it can still optimize the reallocation of a vector); maybe Qt’s definition of trivial relocability does not match ours, and we need to fix our definitions. In this post we will explore these possibilities and reach some conclusions.
Registration is now open for CppCon 2024! The conference starts on September 15 and will be held
Registration is now open for CppCon 2024! The conference starts on September 15 and will be held
In the last post of this series we started exploring how to erase an element from the middle of a vector.
Registration is now open for CppCon 2024! The conference starts on September 15 and will be held
In recent discussions around the use of std::move in C++, questions have arisen regarding its potential overuse and the compiler's treatment of its return values. Addressing concerns raised by developers like Jonathan Duncan, this article delves into the nuances of
Registration is now open for CppCon 2024! The conference starts on September 15 and will be held
Registration is now open for CppCon 2024! The conference starts on September 15 and will be held