Formatting library (C++20)
From cppreference.com
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.
Run this code
#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) |
(C++20) |
writes out formatted representation of its arguments through an output iterator, not exceeding specified size (function template) |
(C++20) |
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) |
(C++20) |
non-template variant of std::format_to using type-erased argument representation (function template) |
(C++20) |
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) |
(C++20)(C++20)(C++20) |
formatting string parser state (class template) |
(C++20)(C++20)(C++20) |
formatting state, including all formatting arguments and the output iterator (class template) |
(C++20) |
argument visitation interface for user-defined formatters (function template) |
(C++20)(C++20) |
creates a type-erased object referencing all formatting arguments, convertible to format_args (function template) |
(C++20)(C++20)(C++20) |
class that provides access to all formatting arguments (class template) |
(C++20) |
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) |