Namespaces
Variants
Views
Actions

Formatting library (since C++20)

From cppreference.com
< cpp‎ | utility
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)  
(C++11)

Relational operators (deprecated in 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

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]
writes out formatted representation of its arguments through an output iterator, not exceeding specified size
(function template) [edit]
determines the number of characters necessary to store the formatted representation of its arguments
(function template) [edit]

[edit] Format strings

Defined in header <format>
class template that performs compile-time format string checks at construction time
(class template) [edit]
creates runtime format strings directly usable in user-oriented formatting functions
(function) [edit]

[edit] Formatting concepts

Defined in header <format>
specifies that a type is formattable, that is, it specializes std::formatter and provides member functions parse and format
(concept) [edit]

[edit] Extensibility support and implementation detail

Defined in header <format>
(C++20)
non-template variant of std::format using type-erased argument representation
(function) [edit]
non-template variant of std::format_to using type-erased argument representation
(function template) [edit]
creates a type-erased object referencing all formatting arguments, convertible to format_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]
class template that helps implementing std::formatter specializations for range types
(class template) [edit]
indicates the argument type can be efficiently printed
(variable template)[edit]
specifies how a range should be formatted
(enum) [edit]
selects a suited std::range_format for a range
(variable template)[edit]
class template that provides access to a formatting argument for user-defined formatters
(class template) [edit]
class that provides access to all formatting arguments
(class template) [edit]
formatting state, including all formatting arguments and the output iterator
(class template) [edit]
formatting string parser state
(class template) [edit]
exception type thrown on formatting errors
(class) [edit]

[edit] Helper items (since C++23)

template< class R, class CharT >

concept /*const-formattable-range*/ =
    ranges::input_range<const R> &&

    std::formattable<ranges::range_reference_t<const R>, CharT>;
(1) (exposition only*)
template< class R, class CharT >

using /*fmt-maybe-const*/ =

    std::conditional_t</*const-formattable-range*/<R, CharT>, const R, R>;
(2) (exposition only*)

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_format 201907L (C++20) Text formatting
202106L (C++20)
(DR)
Compile-time format string checks;
Reducing parameterization of std::vformat_to
202110L (C++20)
(DR)
Fixing locale handling in chrono formatters;
Supporting non-const-formattable types
202207L (C++20)
(DR)
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
__cpp_lib_format_ranges 202207L (C++23) Formatting ranges

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

(C++23)
prints to stdout or a file stream using formatted representation of the arguments
(function template) [edit]
(C++23)
same as std::print except that each print is terminated by additional new line
(function template) [edit]
outputs formatted representation of the arguments
(function template) [edit]