std::range_formatter(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 NAME

okay so bestie, this is the std::range_formatter(3) page from the C++ Standard Library manual, and it's literally std::range_formatter β€” that's the name, that's the vibe, no notes πŸ’…

✨ Synopsis ✨

Defined in header <format> β€” and yes, (since C++23).

template< class T, class CharT = char >

    requires std::same_as<std::remove_cvref_t<T>, T> &&
             std::formattable<T, CharT>

class range_formatter;

The std::range_formatter is a helper class template for implementing std::formatter specializations for range types. helper class template realness 🫢

πŸŽ€ Member objects

  • underlying_ (private) β€” the underlying formatter of type std::formatter<T, CharT> (exposition-only member object*). the main-character formatter underneath ✨
  • separator_ (private) β€” a string representing the separator of the range formatted result. The default separator is ", ". (exposition-only member object*) πŸ“
  • opening-bracket_ (private) β€” a string representing the opening bracket of the range formatted result. The default opening bracket is "[". (exposition-only member object*) πŸŽ€
  • closing-bracket_ (private) β€” a string representing the closing bracket of the range formatted result. The default closing bracket is "]". (exposition-only member object*) πŸ¦‹

🧚 Member functions

  • set_separator β€” sets a specified separator for the range formatted result (public member function). separator glow-up ✨
  • set_brackets β€” sets a specified opening and closing brackets for the range formatted result (public member function). bracket makeover πŸ’…
  • underlying β€” returns the underlying formatter (public member function). underlying formatter reveal πŸ‘€
  • parse β€” parses the format specifier as specified by range-format-spec (public member function). parse era πŸ”₯
  • format β€” writes the range formatted output as specified by range-format-spec (public member function). format main character energy πŸ‘‘

🌟 std::range_formatter::set_separator

constexpr void set_separator( std::basic_string_view<CharT> sep ) noexcept;

Assigns sep to separator_. simple, slay, separator assigned 🫢

🌺 std::range_formatter::set_brackets

constexpr void set_brackets( std::basic_string_view<CharT> opening,
                             std::basic_string_view<CharT> closing ) noexcept;

Assigns opening and closing to opening-bracket_ and closing-bracket_, respectively. bracket assignment besties πŸŽ€

πŸ¦‹ std::range_formatter::underlying

constexpr std::formatter<T, CharT>& underlying();             (1)
constexpr const std::formatter<T, CharT>& underlying() const; (2)

Returns underlying_ (the underlying formatter). underlying formatter, returned with confidence ✨

πŸ’œ 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. parse it, store it, own it πŸ’…

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. required modifications happening ⭐

It calls underlying_.set_debug_format() if:

  • the range-type is neither s nor ?s,
  • underlying_.set_debug_format() is a valid expression, and
  • there is no range-underlying-spec.

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

πŸ”₯ std::range_formatter::format

template< ranges::input_range R, class FormatContext >

    requires std::formattable<ranges::range_reference_t<R>, CharT> &&
             std::same_as<std::remove_cvref_t<ranges::range_reference_t<R>>, T>

auto format( R&& r, FormatContext& ctx ) const -> FormatContext::iterator;

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(). string or escaped-string treatment, iconic ✨

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

  • opening-bracket_,
  • for each formattable element e of the range r:
    • the result of writing e via underlying_, and
    • separator_, unless e is the last element of r, and
  • closing-bracket_.

Returns an iterator past the end of the output range. output range iterator, mwah πŸ’‹

πŸ’… Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. retroactive glow-up history πŸ“

  • LWG 3892 β€” Applied to C++23; behavior as published: the formatting of nested ranges was incorrect; correct behavior: corrected. nested ranges got fixed, no cap 😭

🌈 See also

  • formatter β€” defines formatting rules for a given type (C++20) (class template). the classic formatter icon πŸ‘‘

Source/version: http://cppreference.com; date: 2024.06.10; page: std::range_formatter(3) β€” that's the cute sign-off, bestie, footer details delivered and wrapped with a bow πŸŽ€πŸ’–βœ¨

πŸ“¦ where this page comes from