std::range_formatter - cppreference.com (original) (raw)

std::range_formatter is a helper class template for implementing range std::formatter specializations.

Contents

[edit] Range format specification

The syntax of range-format-spec is:

| | | | | ----------------------------------------------------------------------------------------------------------------------- | | | | range-fill-and-align (optional) width (optional) n(optional) range-type (optional) range-underlying-spec (optional) | | | | | | |

The range-fill-and-align is interpreted the same way as a fill-and-align except that the fill in range-fill-and-align is any character other than **{**, **}**, or **:**.

The width is described in standard format width specification.

The **n** option causes the range to be formatted without the opening and closing brackets.

assert(std::format("{}", views::iota(1, 5)) == "[1, 2, 3, 4]"); assert(std::format("{:n}", views::iota(1, 5)) == "1, 2, 3, 4");

The format-spec in a range-underlying-spec (its syntax is equivalent to : format-spec), if any, is interpreted by the range element formatter std::formatter<T, CharT>.

std::array ints{12, 10, 15, 14};   assert(std::format("{}", ints) == "[12, 10, 15, 14]"); assert(std::format("{::X}", ints) == "[C, A, F, E]"); assert(std::format("{:n:_^4}", ints) == "12, 10, 15, 14");

The range-type changes the way a range is formatted, with certain options only valid with certain argument types.

The available range presentation types are:

std::array char_pairs { std::pair{'A', 5}, std::pair{'B', 10}, std::pair{'C', 12} };   assert(std::format("{}", char_pairs) == "[('A', 5), ('B', 10), ('C', 12)]"); assert(std::format("{:m}", char_pairs) == "{'A': 5, 'B': 10, 'C': 12}");

[edit] Data members

Member name Definition
std::formatter<T, CharT> underlying_ (private) the underlying formatter for elements(exposition-only member object*)
std::basic_string_view<CharT> separator_ (private) a string representing the separator of the range formatted result. The default separator is ", ".(exposition-only member object*)
std::basic_string_view<CharT> opening-bracket_ (private) a string representing the opening bracket of the range formatted result. The default opening bracket is "[".(exposition-only member object*)
std::basic_string_view<CharT> closing-bracket_ (private) a string representing the closing bracket of the range formatted result. The default closing bracket is "]".(exposition-only member object*)

[edit] Member functions

| | sets a specified separator for the range formatted result (public member function) | | -------------------------------------------------------------------------------------------------------- | | | sets a specified opening and closing brackets for the range formatted result (public member function) | | | returns the underlying formatter (public member function) | | | parses the format specifier as specified by range-format-spec (public member function) | | | writes the range formatted output as specified by range-format-spec (public member function) |

std::range_formatter::set_separator

Assigns sep to _separator_.

std::range_formatter::set_brackets

Assigns opening and closing to _opening-bracket_ and _closing-bracket_, respectively.

std::range_formatter::underlying

Returns _underlying_ (the underlying formatter).

std::range_formatter::parse

| template< class ParseContext > constexpr auto parse( ParseContext& ctx ) -> ParseContext::iterator; | | | | ------------------------------------------------------------------------------------------------------ | | |

Parses the format specifiers as a range-format-spec and stores the parsed specifiers in the current object.

Calls _underlying_.parse(ctx) to parse format-spec in range-format-spec or, if the latter is not present, an empty format-spec.

If range-type or the **n** option is present, the values of _opening-bracket_, _closing-bracket_, and _separator_ are modified as required.

It calls _underlying_.set_debug_format() if:

Returns an iterator past the end of the range-format-spec.

std::range_formatter::format

If the range-type was either **s** or **?s**, it writes the formatted std::basic_string<CharT>(std::from_range, r) as a string or an escaped string, respectively, into ctx.out().

Otherwise, it writes the following into ctx.out() as specified by range-format-spec, in order:

Returns an iterator past the end of the output range.

[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 3892 C++23 the formatting of nested ranges was incorrect corrected

[edit] See also

| | defines formatting rules for a given type (class template) [edit] | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |