Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::formatter<pair-or-tuple>

      From cppreference.com
      <cpp‎ |utility‎ |format
       
       
       
      Formatting library
      Standard format specification
      Formatting functions
      (C++20)
      (C++20)
      (C++20)
      (C++20)
      Format strings
      Formatting concepts
      Formatter
      (C++20)
      formatter<pair-or-tuple>
      (C++23)
      Formatting arguments
      (C++20)(deprecated in C++26)
      Format error
       
      Defined in header<format>
      template<class CharT,std::formattable<CharT>...Ts>
      struct formatter</*pair-or-tuple*/<Ts...>, CharT>;
      (since C++23)

      The template specialization ofstd::formatter for thestd::pair andstd::tuple allows users to convert a pair or a tuple to its textual representation as a collection of elements usingformatting functions.

      The exposition-only name/*pair-or-tuple*/ denotes either class templatestd::pair orstd::tuple.

      This specialization meets theFormatter requirements if(std::formattable<const Ts, CharT>&& ...) istrue. It always meets theBasicFormatter requirements.

      Contents

      [edit]Format specification

      The syntax oftuple-format-spec is:

      tuple-fill-and-align (optional)width (optional)tuple-type (optional)

      Thetuple-fill-and-align is interpreted the same way as afill-and-align except that thefill intuple-fill-and-align is any character other than{,}, or:.

      Thewidth is described instandard format width specification.

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

      The available tuple presentation types are:

      • m: Indicates that both opening and closing brackets should be"" while the separator should be": ".
      • Ifm is chosen as thetuple-type, the program is ill-formed unlesssizeof...(Ts)==2 istrue.
      • n: Indicates that separator, opening and closing brackets should be"".

      [edit]Member objects

      Member name Definition
      underlying_(private) tuple of underlying formatters of typestd::tuple<std::formatter<std::remove_cvref_t<Ts>, CharT>...>
      (exposition-only member object*)
      separator_(private) a string representing the separator of the tuple formatted result (defaults to", ")
      (exposition-only member object*)
      opening-bracket_(private) a string representing the opening bracket of the tuple formatted result (defaults to"(")
      (exposition-only member object*)
      closing-bracket_(private) a string representing the closing bracket of the tuple formatted result (defaults to")")
      (exposition-only member object*)

      [edit]Member functions

      set_separator
      sets a specified separator for the tuple formatted result
      (public member function)
      set_brackets
      sets a specified opening and closing brackets for the tuple formatted result
      (public member function)
      parse
      parses the format specifier as specified bytuple-format-spec
      (public member function)
      format
      writes the tuple formatted output as specified bytuple-format-spec
      (public member function)

      std::formatter<pair-or-tuple>::set_separator

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

      Assignssep toseparator_.

      std::formatter<pair-or-tuple>::set_brackets

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

      Assignsopening andclosing toopening-bracket_ andclosing-bracket_, respectively.

      std::formatter<pair-or-tuple>::parse

      template<class ParseContext>
      constexprauto parse( ParseContext& ctx)-> ParseContext::iterator;

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

      Iftuple-type or then option is present, the values ofopening-bracket,closing-bracket, andseparator are modified as required.

      For each elemente inunderlying_, callse.parse(ctx) to parse an emptyformat-spec and, ife.set_debug_format() is a valid expression, callse.set_debug_format().

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

      std::formatter<pair-or-tuple>::format

      template<class FormatContext>

      FormatContext::iterator

      format(/*maybe-const-pair-or-tuple*/<Ts...>& elems, FormatContext& ctx)const;

      /*maybe-const-pair-or-tuple*/ denotes:

      • const/*pair-or-tuple*/, if(std::formattable<const Ts, CharT>&& ...) istrue,
      • /*pair-or-tuple*/ otherwise.

      Writes the following intoctx.out() as specified bytuple-format-spec, in order:

      • opening-bracket_,
      • for each indexI in[0sizeof...(Ts)):
      • ifI!=0,separator_,
      • the result of writingstd::get<I>(elems) viastd::get<I>(underlying_), and
      • closing-bracket_.

      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.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3892C++23the formatting of nested tuples was incorrectcorrected

      [edit]See also

      (C++20)
      defines formatting rules for a given type
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/format/tuple_formatter&oldid=171289"

      [8]ページ先頭

      ©2009-2025 Movatter.jp