The Formatting Library in C++20
This post is a cross-post from www.ModernesCpp.com.
Today, I will start a series about the formatting library in C++20. The series is based on my C++20 book.
Although Peter Gottschling wrote two great blog posts about the formatting library in C++20 (“std::format in C++20“, “C++20: Extend std::format for User-Defined Types“, I will write about the formatting library again. The reason is simple: Peter’s post gave you a great introduction and overview. I want to present all the details so that you can use this post and the upcoming ones as a reference.
C++20 supports the following formatting functions:
The functions std::format and std::format_to are functionally equivalent to their pendants std::vformat and std::vformat_to, but they differ in a few points:
The formatting functions accept an arbitrary number of arguments. The following program gives a first impression of the functions std::format, std::format_to, and std::format_to_n.
// format.cpp
#include <format>
#include <iostream>
#include <iterator>
#include <string>
int main() {
std::cout << '\n';
std::cout << std::format("Hello, C++{}!\n", "20") << '\n'; // (1)
std::string buffer;
std::format_to( // (2)
std::back_inserter(buffer),
"Hello, C++{}!\n",
"20");
std::cout << buffer << '\n';
buffer.clear();
std::format_to_n( // (3)
std::back_inserter(buffer), 5,
"Hello, C++{}!\n",
"20");
std::cout << buffer << '\n';
std::cout << '\n';
}
The program directly displays on line (1) the formatted string. However, the calls on lines (2) and (3) use a string as a buffer. Additionally, std::format_to_n pushes only five characters onto the buffer.
Accordingly, here is the corresponding program using std::vformat and std::vformat_n.
// formatRuntime.cpp
#include <format>
#include <iostream>
#include <iterator>
#include <string>
int main() {
std::cout << '\n';
std::string formatString = "Hello, C++{}!\n";
std::cout << std::vformat(formatString, std::make_format_args("20")) << '\n'; // (1)
std::string buffer;
std::vformat_to( // (2)
std::back_inserter(buffer),
formatString,
std::make_format_args("20"));
std::cout << buffer << '\n';
}
The formatString in lines (1) and (2) is a lvalue.
Presumably, the most exciting part of the formatting functions is the format string (“Hello, C++{}!\n“).
Format String
The formatting string syntax is identical for the formatting functions std::format, std::format_to, std::format_to_n, std::vformat, and std::vformat_to. I use std::format in my examples.
The format string FormatString consists of
A replacement field has the format { }.
?
Modernes C++ Mentoring
Be part of my mentoring programs:
Do you want to stay informed about my mentoring programs: Subscribe via E-Mail.
?
The argument id allows you to specify the index of the arguments in Args. The ids start with 0. When you don’t provide the argument id, the fields are filled in the same order as the arguments are given. Either all replacement fields have to use an argument id or none; i.e., std::format("{}, {}", "Hello", "World") and std::format("{1}, {0}", "World", "Hello") will both compile, but std::format("{1}, {}", "World", "Hello") won’t. ?
领英推荐
std::formatter and its specializations define the format specification for the argument types.
The fact that the format strings is a compile time variable, has two interesting consequences: performance and safety.
Compile Time
What’s Next?
I will use the next blog post to fill in the theory with practice.
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
Seminars
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
New
Contact Me