Namespaces
Variants
Views
Actions

std::format

From cppreference.com
< cpp‎ | utility‎ | format
Revision as of 06:57, 12 November 2023 by Studyingegret (Talk | contribs)

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <format>
template< class... Args >
std::string format( std::format_string<Args...> fmt, Args&&... args );
(1) (since C++20)
template< class... Args >
std::wstring format( std::wformat_string<Args...> fmt, Args&&... args );
(2) (since C++20)
template< class... Args >

std::string format( const std::locale& loc,

                    std::format_string<Args...> fmt, Args&&... args );
(3) (since C++20)
template< class... Args >

std::wstring format( const std::locale& loc,

                     std::wformat_string<Args...> fmt, Args&&... args );
(4) (since C++20)

Format args according to the format string fmt, and return the result as a string. If present, loc is used for locale-specific formatting. Performs a compile-time validity check on the format string.

1) Equivalent to return std::vformat(fmt.get(), std::make_format_args(args...));.
2) Equivalent to return std::vformat(fmt.get(), std::make_wformat_args(args...));.
3) Equivalent to return std::vformat(loc, fmt.get(), std::make_format_args(args...));.
4) Equivalent to return std::vformat(loc, fmt.get(), std::make_wformat_args(args...));.

Below, let CharT be char for overloads (1,3), wchar_t for overloads (2,4).

The behavior is undefined if std::formatter<Ti, CharT> does not meet the BasicFormatter requirements for any Ti in Args (as required by std::make_format_args and std::make_wformat_args).

Since P2216R3, at compile time, std::format checks if fmt is a valid format string for the types Args (but not touching the values), via the helper type std::basic_format_string. If an error is found, it will be treated as a compilation error. To support this,

  • for each type T in Args, std::formatter<T, CharT>::parse() must be constexpr; and
  • it is an error to provide a format string that is not a compile-time constant.

std::vformat does not perform this compile-time check and thus does not have the above two requirements.

Contents

Parameters

fmt - an object that represents the format string. The format string consists of
  • ordinary characters (except { and }), which are copied unchanged to the output,
  • escape sequences {{ and }}, which are replaced with { and } respectively in the output, and
  • replacement fields.

Each replacement field has the following format:

{ arg-id (optional) } (1)
{ arg-id (optional) : format-spec } (2)
1) replacement field without a format specification
2) replacement field with a format specification
arg-id - specifies the index of the argument in args whose value is to be used for formatting; if it is omitted, the arguments are used in order.

The arg-id s in a format string must all be present or all be omitted. Mixing manual and automatic indexing is an error.

format-spec - the format specification defined by the std::formatter specialization for the corresponding argument. Cannot start with }.

(since C++23)
(since C++26)
  • For other formattable types, the format specification is determined by user-defined formatter specializations.
args... - arguments to be formatted
loc - std::locale used for locale-specific formatting

Return value

A string object holding the formatted result.

Exceptions

Throws std::bad_alloc on allocation failure. Also propagates exception thrown by any formatter.

Notes

It is not an error to provide more arguments than the format string requires:

std::format("{} {}!", "Hello", "world", "something"); // OK, produces "Hello world!"

As of P2216R3, it is an error if the format string is not a constant expression. std::vformat can be used in this case.

std::string f(std::string_view runtime_format_string)
{
    // return std::format(runtime_format_string, "foo", "bar"); // error
    return std::vformat(runtime_format_string, std::make_format_args("foo", "bar")); // ok
}

Example

#include <format>
#include <iostream>
#include <string>
#include <string_view>
 
template<typename... Args>
std::string dyna_print(std::string_view rt_fmt_str, Args&&... args)
{
    return std::vformat(rt_fmt_str, std::make_format_args(args...));
}
 
int main()
{
    std::cout << std::format("Hello {}!\n", "world");
 
    std::string fmt;
    for (int i{}; i != 3; ++i)
    {
        fmt += "{} "; // constructs the formatting string
        std::cout << fmt << " : ";
        std::cout << dyna_print(fmt, "alpha", 'Z', 3.14, "unused");
        std::cout << '\n';
    }
}

Output:

Hello world!
{}  : alpha
{} {}  : alpha Z
{} {} {}  : alpha Z 3.14

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
P2216R3 C++20 throws std::format_error for invalid format string invalid format string results in compile-time error
P2418R2 C++20 objects that are neither const-usable nor copyable
(such as generator-like objects) are not formattable
allow formatting these objects
P2508R1 C++20 there's no user-visible name for this facility the name basic_format_string is exposed

See also

(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]