Section

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.

# Declarations

template< class R, class CharT >
concept /*const-formattable-range*/ =
ranges::input_range<const R> &&
std::formattable<ranges::range_reference_t<const R>, CharT>;

(exposition only*)

template< class R, class CharT >
using /*fmt-maybe-const*/ =
std::conditional_t</*const-formattable-range*/<R, CharT>, const R, R>;

(exposition only*)

# Notes

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.

# Example

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

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
P2418R2C++20objects that are neither const-formattable nor copyable(such as generator-like objects) are not formattableallow formatting these objects(relaxed formatter requirements)
P2508R1C++20there’s no user-visible name for this facilitythe name basic_format_string is exposed

# See also