Formatting library (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)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
Formatting library
Formatting functions
(C++20)
(C++20)
(C++20)
(C++20)
Formatter
(C++20)
Formatting arguments
Format error
 

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 and reuse some of its infrastructure such as overloaded insertion operators for user-defined types.

#include <format>
#include <cassert>
int main() {
    std::string message = std::format("The answer is {}.", 42);
    assert( message == "The answer is 42." );
}

Formatting functions

(C++20)
stores formatted representation of the arguments in a new string
(function template)
(C++20)
writes out formatted representation of its arguments through an output iterator
(function template)
writes out formatted representation of its arguments through an output iterator, not exceeding specified size
(function template)
determines the number of characters necessary to store the formatted representation of its arguments
(function template)

Extensibility support and implementation detail

(C++20)
non-template variant of std::format using type-erased argument representation
(function)
non-template variant of std::format_to using type-erased argument representation
(function template)
class template that provides access to a formatting argument for user-defined formatters
(class template)
(C++20)
class template that defines formatting rules for a given type
(class template)
formatting string parser state
(class template)
formatting state, including all formatting arguments and the output iterator
(class template)
argument visitation interface for user-defined formatters
(function template)
creates a type-erased object referencing all formatting arguments, convertible to format_args
(function template)
class that provides access to all formatting arguments
(class template)
exception type thrown on formatting errors
(class)

Notes

Feature-test macro: __cpp_lib_format

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)