Formatting library (since C++20)
The text formatting library offers a safe and extensible alternative to the printf family of functions. It is intended to complement the existing C++ I/O streams library.
Contents
[edit] Format specifications
Format specification specifies how objects are formatted with different kinds of options.
The formatting of objects of basic types and standard string types uses the basic format specification. Other library components may also provide their own format specifications, see here for details.
[edit] Formatting functions
<format>
(function template) [edit]
(function template) [edit]
(function template) [edit]
[edit] Format strings
<format>
(class template) [edit]
(function) [edit]
[edit] Formatting concepts
<format>
parse
and format
(concept) [edit]
[edit] Extensibility support and implementation detail
<format>
(function) [edit]
(function template) [edit]
format_args
(function template) [edit]
(function template) [edit]
(class template) [edit]
(class template) [edit]
(class template) [edit]
(class template) [edit]
[edit] Helper items (since C++23)
concept /*const-formattable-range*/ =
ranges::input_range <const R> &&
using /*fmt-maybe-const*/ =
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_format |
201907L |
(C++20) | Text formatting |
202106L |
(C++23) (DR20) |
Compile-time format string checks; Reducing parameterization of std::vformat_to | |
202110L |
(C++23) (DR20) |
Fixing locale handling in chrono formatters; Supporting non-const-formattable types | |
202207L |
(C++23) (DR20) |
Exposing std::basic_format_string; Clarify handling of encodings in localized formatting of chrono types | |
202304L |
(C++26) | Formatting pointers | |
202305L |
(C++26) | Type-checking format args | |
202306L |
(C++26) | Member std::basic_format_arg::visit
| |
202311L |
(C++26) | Runtime format string | |
202403L |
(C++26) | Printing Blank Lines with std::println | |
202403L |
(C++26) (DR23) |
Permit an efficient implementation of std::print This section is incomplete
Reason: the value should be >= 202406L, i.e. be different from the previous one | |
__cpp_lib_format_ranges |
202207L |
(C++23) | Formatting ranges |
__cpp_lib_format_path |
202403L |
(C++26) | Formatting of std::filesystem::path |
__cpp_lib_format_uchar |
202311L |
(C++26) | Fix formatting of code units as integers |
__cpp_lib_formatters |
202302L |
(C++23) | Formatting std::thread::id and std::stacktrace |
We intentionally treat the addition of std::basic_format_string
(P2508) as a defect report because all known implementations make these components available in C++20 mode, although it is not so categorized officially.
[edit] Example
#include <cassert> #include <format> int main() { std::string message = std::format ("The answer is {}.", 42); assert (message == "The answer is 42."); }
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
P2418R2 | C++20 | objects that are neither const-formattable nor copyable (such as generator-like objects) are not formattable |
allow formatting these objects (relaxed formatter requirements) |
P2508R1 | C++20 | there's no user-visible name for this facility | the name basic_format_string is exposed
|
[edit] See also
(function template) [edit]
(function template) [edit]