std::formatter
<format>
struct formatter;
The enabled specializations of std::formatter
define formatting rules for a given type. Enabled specializations meet the BasicFormatter requirements, and, unless otherwise specified, also meet the Formatter requirements.
For all types T
and CharT
for which no specialization std::formatter<T, CharT>
is enabled, that specialization is a complete type and is disabled.
Disabled specializations do not meet the Formatter requirements, and the following are all false:
- std::is_default_constructible_v
- std::is_copy_constructible_v
- std::is_move_constructible_v
- std::is_copy_assignable_v
- std::is_move_assignable_v .
Contents
[edit] Basic standard specializations
In the following list, CharT
is either char or wchar_t, ArithmeticT
is any cv-unqualified arithmetic type other than char, wchar_t, char8_t, char16_t, or char32_t:
struct formatter<char, char>;
struct formatter<char, wchar_t>;
struct formatter<wchar_t, wchar_t>;
struct formatter<CharT*, CharT>;
struct formatter<const CharT*, CharT>;
struct formatter<CharT[N], CharT>;
struct formatter<std::basic_string <CharT, Traits, Alloc>, CharT>;
struct formatter<std::basic_string_view <CharT, Traits>, CharT>;
struct formatter<ArithmeticT, CharT>;
struct formatter<std::nullptr_t, CharT>;
struct formatter<void*, CharT>;
struct formatter<const void*, CharT>;
Formatters for other pointers and pointers to members are disabled.
Specializations such as std::formatter<wchar_t, char> and std::formatter<const char*, wchar_t> that would require encoding conversions are disabled.
The following specialization are still disabled in C++23 to avoid formatting some char sequences as ranges of wchar_t:
struct formatter<char*, wchar_t>;
struct formatter<const char*, wchar_t>;
struct formatter<char[N], wchar_t>;
struct formatter<std::basic_string <char, Traits, Allocator>, wchar_t>;
struct formatter<std::basic_string_view <char, Traits>, wchar_t>;
A debug-enabled formatter specialization additionally provides a public non-static member function constexpr void set_debug_format(); which modifies the state of the formatter object so that it will format the values as escaped and quoted, as if the type of the format specifier parsed by the last call to parse
were ?
.
Each formatter specialization for string or character type is debug-enabled.
[edit] Standard format specification
Reason: The standard format specification is moved to a separate page. The section title is temporarily preserved for links to this section. This section will be removed after all those links are settled.
[edit] Standard specializations for library types
[edit] Example
#include <algorithm> #include <format> #include <iomanip> #include <iostream> #include <sstream> #include <string_view> struct QuotableString : std::string_view {}; template<> struct std::formatter<QuotableString, char> { bool quoted = false; template<class ParseContext> constexpr ParseContext::iterator parse(ParseContext& ctx) { auto it = ctx.begin(); if (it == ctx.end()) return it; if (*it == '#') { quoted = true; ++it; } if (it != ctx.end() && *it != '}') throw std::format_error ("Invalid format args for QuotableString."); return it; } template<class FmtContext> FmtContext::iterator format(QuotableString s, FmtContext& ctx) const { std::ostringstream out; if (quoted) out << std::quoted (s); else out << s; return std::ranges::copy (std::move(out).str(), ctx.out()).out; } }; int main() { QuotableString a("be"), a2(R"( " be " )"); QuotableString b("a question"); std::cout << std::format ("To {0} or not to {0}, that is {1}.\n", a, b); std::cout << std::format ("To {0:} or not to {0:}, that is {1:}.\n", a, b); std::cout << std::format ("To {0:#} or not to {0:#}, that is {1:#}.\n", a2, b); }
Output:
To be or not to be, that is a question. To be or not to be, that is a question. To " \" be \" " or not to " \" be \" ", that is "a question".
[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 |
---|---|---|---|
LWG 3944 | C++23 | some char sequences were formattable as ranges of wchar_t | disable specializations added |
[edit] See also
(class template) [edit]
parse
and format
(concept) [edit]
(class template) [edit]