Localization library | |||||||||||||||||||||||||
Regular expressions library(C++11) | |||||||||||||||||||||||||
Formatting library(C++20) | |||||||||||||||||||||||||
Null-terminated sequence utilities | |||||||||||||||||||||||||
Byte strings | |||||||||||||||||||||||||
Multibyte strings | |||||||||||||||||||||||||
Wide strings | |||||||||||||||||||||||||
Primitive numeric conversions | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
Text encoding identifications | |||||||||||||||||||||||||
|
Standard format specification | ||||
Formatting functions | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
Format strings | ||||
(C++20)(C++20)(C++20) | ||||
(C++26) | ||||
Formatting concepts | ||||
(C++23) | ||||
Formatter | ||||
(C++20) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++20)(C++20)(C++20) | ||||
(C++20)(C++20)(C++20) | ||||
(C++23) | ||||
(C++23) | ||||
Formatting arguments | ||||
(C++20) | ||||
(C++20) | ||||
(C++20)(C++20)(C++20) | ||||
(C++20)(deprecated in C++26) | ||||
(C++20)(C++20) | ||||
Format error | ||||
(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 |
Format specification specifies how objects are formatted with different kinds of options.
The formatting of objects of basic types and standard string types uses thebasic format specification. Other library components may also provide their own format specifications, seehere for details.
Defined in header <format> | |
(C++20) | stores formatted representation of the arguments in a new string (function template)[edit] |
(C++20) | writes out formatted representation of its arguments through an output iterator (function template)[edit] |
(C++20) | writes out formatted representation of its arguments through an output iterator, not exceeding specified size (function template)[edit] |
(C++20) | determines the number of characters necessary to store the formatted representation of its arguments (function template)[edit] |
Defined in header <format> | |
(C++20)(C++20)(C++20) | class template that performs compile-time format string checks at construction time (class template)[edit] |
(C++26) | creates runtime format strings directly usable in user-oriented formatting functions (function)[edit] |
Defined in header <format> | |
(C++23) | specifies that a type is formattable, that is, it specializesstd::formatter and provides member functionsparse andformat (concept)[edit] |
Defined in header <format> | |
(C++20) | non-template variant ofstd::format using type-erased argument representation (function)[edit] |
(C++20) | non-template variant ofstd::format_to using type-erased argument representation (function template)[edit] |
(C++20)(C++20) | creates a type-erased object referencing all formatting arguments, convertible toformat_args (function template)[edit] |
(C++20)(deprecated in C++26) | argument visitation interface for user-defined formatters (function template)[edit] |
(C++20) | defines formatting rules for a given type (class template)[edit] |
(C++23) | class template that helps implementingstd::formatter specializations for range types (class template)[edit] |
indicates the argument type can be efficiently printed (variable template)[edit] | |
(C++23) | specifies how a range should be formatted (enum)[edit] |
(C++23) | selects a suitedstd::range_format for a range (variable template)[edit] |
(C++20) | class template that provides access to a formatting argument for user-defined formatters (class template)[edit] |
(C++20)(C++20)(C++20) | class that provides access to all formatting arguments (class template)[edit] |
(C++20)(C++20)(C++20) | formatting state, including all formatting arguments and the output iterator (class template)[edit] |
(C++20)(C++20)(C++20) | formatting string parser state (class template)[edit] |
(C++20) | exception type thrown on formatting errors (class)[edit] |
template<class R,class CharT> concept/*const-formattable-range*/= | (1) | (exposition only*) |
template<class R,class CharT> using/*fmt-maybe-const*/= | (2) | (exposition only*) |
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 ofstd::vformat_to | ||
202110L | (C++23) (DR20) | Fixing locale handling in chrono formatters; Supporting non-const-formattable types | ||
202207L | (C++23) (DR20) | Exposingstd::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) | Memberstd::basic_format_arg::visit | ||
202311L | (C++26) | Runtime format string | ||
202403L | (C++26) | Printing Blank Lines withstd::println | ||
202403L | (C++26) (DR23) | Permit an efficient implementation ofstd::print
| ||
__cpp_lib_format_ranges | 202207L | (C++23) | Formatting ranges | |
__cpp_lib_format_path | 202403L | (C++26) | Formatting ofstd::filesystem::path | |
__cpp_lib_format_uchar | 202311L | (C++26) | Fix formatting of code units as integers | |
__cpp_lib_formatters | 202302L | (C++23) | Formattingstd::thread::id andstd::stacktrace |
We intentionally treat the addition ofstd::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.
#include <cassert>#include <format> int main(){std::string message=std::format("The answer is {}.",42);assert(message=="The answer is 42.");}
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 namebasic_format_string is exposed |
(C++23) | prints tostdout or a file stream usingformatted representation of the arguments (function template)[edit] |
(C++23) | same asstd::print except that each print is terminated by additional new line (function template)[edit] |
(C++23) | outputsformatted representation of the arguments (function template)[edit] |