Peter Gottschling presented in his last post “std::format in C++20” the basics of the new formatting library in C++20. In today’s post, Peter writes about the formatting of user-defined types.
Our first example of template specialization is customizing the new format library introduced to support user types.
Formatting User-Defined Types
For example, we choose the
dmc is the namespace from the book “Discovering Modern C++” by the author) class for which we like to specify the formatting of the single values. In addition, we want to replace the enclosing brackets with curly braces when the format string contains the letter
'c'. To this end, we have to specialize the class
fmt::formatter for the prototype library
fmt). Our specialization shall contain the methods
Let’s start with the former:
As an argument, the parse context is given whose
begin iterator points to the first character of the format specification, i.e.~the first character after the colon and, in its absence, the first character after the opening brace. We copy the format specification almost identically to our local
value_format, only our unique character
'c' is skipped. For simplicity, we assume that the format doesn’t contain any opening or closing brace, so the next closing brace terminates our format string. Finally, we return the iterator pointing to the closing brace or the end iterator.
With this information, we can output our
vector in the method
First, we take a reference to the output buffer. Then we write the opening brace or bracket to it. Since braces have a special meaning in the
format library, we need an escape sequence of double braces. The remaining output is equivalent to the
ostream output. Finally, we return the output buffer.
Now we can try various formats:
and see the outputs:
Altogether, since the new formatting is:
- Compact: demonstrated in the examples above
- Adaptable: to various output orders
- Type-safe: an exception is thrown when an argument doesn’t match
- Extensible: can be extended to user-defined types
For those reasons, it is superior to the preceding techniques, and we strongly advise using it as soon as sufficient compiler support is available.
Thanks once more to Peter Gottschling for providing a compact introduction to
std::format. Let me add a few words to complete his introduction to the formatting library.
Try It Out
As Peter mentioned, the GitHub-hosted
fmt library is a prototype for the new formatting library in C++20. The project’s front page includes a few straightforward examples and performance numbers. These examples include a direct link to the compiler explorer for executing the example.
Thanks to the new formatting library, you can display time durations of the
Executing the program on the compiler explorer gives you the following output:
Porting to C++20
Porting the previous program from
fmt the C++20 format library is a piece of cake. You have to use the C++ standard header
iostream. Additionally, replace the call
fmt::print with the function
std::format and push the result to
std::format returns a string according to the given format string and an optional local.
In my next post, I will continue with the convenience functions. With C++20, you can calculate the midpoint of two values, check if a
std::string start or ends with a substring, and create callables with
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,