Namespaces
Variants

Standard library header <format> (C++20)

From cppreference.net
Standard library headers

Dieser Header ist Teil der Textverarbeitungsbibliothek .

Inhaltsverzeichnis

Konzepte

Spezifiziert, dass ein Typ formatierbar ist, d.h. er spezialisiert std::formatter und stellt die Memberfunktionen parse und format bereit
(Konzept)

Enumerationen

gibt an, wie ein Bereich formatiert werden soll
(enum)

Klassen

(C++20)
definiert Formatierungsregeln für einen gegebenen Typ
(Klassentemplate)
Klassentemplate, das bei der Implementierung von std::formatter Spezialisierungen für Bereichstypen hilft
(Klassentemplate)
Formatierungszeichenketten-Parserzustand
(Klassentemplate)
Formatierungszustand, einschließlich aller Formatierungsargumente und des Ausgabeiterators
(Klassentemplate)
Klassentemplate, das Zugriff auf ein Formatierungsargument für benutzerdefinierte Formatierer bereitstellt
(Klassentemplate)
Klasse, die Zugriff auf alle Formatierungsargumente bereitstellt
(Klassen-Template)
Klassentemplate, das zur Erstellungszeit Überprüfungen der Formatzeichenkette zur Compilezeit durchführt
(Klassentemplate)
Ausnahmetyp, der bei Formatierungsfehlern ausgelöst wird
(Klasse)
Formatter-Spezialisierungen
Formatierungsunterstützung für pair und tuple
(Klassen-Template-Spezialisierung)
Formatierungsunterstützung für Ranges
(Klassen-Template-Spezialisierung)

Funktionen

(C++20)
speichert die formatierte Darstellung der Argumente in einer neuen Zeichenkette
(Funktionsschablone)
(C++20)
Schreibt eine formatierte Darstellung seiner Argumente über einen Ausgabeiterator
(Funktionsschablone)
schreibt eine formatierte Darstellung seiner Argumente über einen Ausgabeiterator, ohne eine bestimmte Größe zu überschreiten
(Funktionsschablone)
bestimmt die Anzahl der Zeichen, die notwendig sind, um die formatierte Darstellung ihrer Argumente zu speichern
(Funktions-Template)
erstellt Laufzeit-Formatzeichenfolgen, die direkt in benutzerorientierten Formatierungsfunktionen verwendet werden können
(Funktion)
(C++20)
Nicht-Template-Variante von std::format mit typeradierten Argumentdarstellungen
(Funktion)
(C++20)
Nicht-Template-Variante von std::format_to mit typeradierten Argumentdarstellung
(Funktionstemplate)
(C++20) (veraltet in C++26)
Argument-Besuchsschnittstelle für benutzerdefinierte Formatierer
(Funktions-Template)
erstellt ein typloses Objekt, das alle Formatierungsargumente referenziert, konvertierbar zu format_args
(Funktions-Template)

Helfer

wählt ein geeignetes std::range_format für einen Bereich aus
(Variablen-Template)
zeigt an, dass der Argumenttyp effizient ausgegeben werden kann
(Variablen-Template)

Übersicht

namespace std {
  // Klassentemplate basic_format_context
  template<class Out, class CharT> class basic_format_context;
  using format_context = basic_format_context</* nicht spezifiziert */, char>;
  using wformat_context = basic_format_context</* nicht spezifiziert */, wchar_t>;
  // Klassentemplate basic_format_args
  template<class Context> class basic_format_args;
  using format_args = basic_format_args<format_context>;
  using wformat_args = basic_format_args<wformat_context>;
  // Klassentemplate basic_format_string
  template<class CharT, class... Args> struct basic_format_string;
  template<class CharT> struct /*Laufzeit-Format-String*/
  {                                   // exposition-only
  private:
    basic_string_view<CharT> /*str*/; // exposition-only
  public:
    /*Laufzeit-Format-String*/(basic_string_view<CharT> s) noexcept
      : /*str*/(s)
    {
    }
    /*Laufzeit-Format-String*/(const /*Laufzeit-Format-String*/&) = delete;
    /*Laufzeit-Format-String*/& operator=(const /*Laufzeit-Format-String*/&) = delete;
  };
  /*Laufzeit-Format-String*/<char> runtime_format(string_view fmt) noexcept { return fmt; }
  /*Laufzeit-Format-String*/<wchar_t> runtime_format(wstring_view fmt) noexcept
  {
    return fmt;
  }
  template<class... Args>
  using format_string = basic_format_string<char, type_identity_t<Args>...>;
  template<class... Args>
  using wformat_string = basic_format_string<wchar_t, type_identity_t<Args>...>;
  // Formatierungsfunktionen
  template<class... Args> string format(format_string<Args...> fmt, Args&&... args);
  template<class... Args> wstring format(wformat_string<Args...> fmt, Args&&... args);
  template<class... Args>
  string format(const locale& loc, format_string<Args...> fmt, Args&&... args);
  template<class... Args>
  wstring format(const locale& loc, wformat_string<Args...> fmt, Args&&... args);
  string vformat(string_view fmt, format_args args);
  wstring vformat(wstring_view fmt, wformat_args args);
  string vformat(const locale& loc, string_view fmt, format_args args);
  wstring vformat(const locale& loc, wstring_view fmt, wformat_args args);
  template<class Out, class... Args>
  Out format_to(Out out, format_string<Args...> fmt, Args&&... args);
  template<class Out, class... Args>
  Out format_to(Out out, wformat_string<Args...> fmt, Args&&... args);
  template<class Out, class... Args>
  Out format_to(Out out, const locale& loc, format_string<Args...> fmt, Args&&... args);
  template<class Out, class... Args>
  Out format_to(Out out, const locale& loc, wformat_string<Args...> fmt, Args&&... args);
  template<class Out> Out vformat_to(Out out, string_view fmt, format_args args);
  template<class Out> Out vformat_to(Out out, wstring_view fmt, wformat_args args);
  template<class Out>
  Out vformat_to(Out out, const locale& loc, string_view fmt, format_args args);
  template<class Out>
  Out vformat_to(Out out, const locale& loc, wstring_view fmt, wformat_args args);
  template<class Out> struct format_to_n_result
  {
    Out out;
    iter_difference_t<Out> size;
  };
  template<class Out, class... Args>
  format_to_n_result<Out> format_to_n(Out out,
                                      iter_difference_t<Out> n,
                                      format_string<Args...> fmt,
                                      Args&&... args);
  template<class Out, class... Args>
  format_to_n_result<Out> format_to_n(Out out,
                                      iter_difference_t<Out> n,
                                      wformat_string<Args...> fmt,
                                      Args&&... args);
  template<class Out, class... Args>
  format_to_n_result<Out> format_to_n(Out out,
                                      iter_difference_t<Out> n,
                                      const locale& loc,
                                      format_string<Args...> fmt,
                                      Args&&... args);
  template<class Out, class... Args>
  format_to_n_result<Out> format_to_n(Out out,
                                      iter_difference_t<Out> n,
                                      const locale& loc,
                                      wformat_string<Args...> fmt,
                                      Args&&... args);
  template<class... Args>
  size_t formatted_size(format_string<Args...> fmt, Args&&... args);
  template<class... Args>
  size_t formatted_size(wformat_string<Args...> fmt, Args&&... args);
  template<class... Args>
  size_t formatted_size(const locale& loc, format_string<Args...> fmt, Args&&... args);
  template<class... Args>
  size_t formatted_size(const locale& loc, wformat_string<Args...> fmt, Args&&... args);
  // Formatter
  template<class T, class CharT = char> struct formatter;
  // Formatter-Sperrung
  template<class T> constexpr bool enable_nonlocking_formatter_optimization = false;
  // Konzept formattable
  template<class T, class CharT>
  concept formattable = /* siehe Beschreibung */;
  template<class R, class CharT>
  concept /*const-formattable-range*/ = // exposition-only
    ranges::input_range<const R> &&
    formattable<ranges::range_reference_t<const R>, CharT>;
  template<class R, class CharT>
  using /*fmt-maybe-const*/ =           // exposition-only
    conditional_t</*const-formattable-range*/<R, CharT>, const R, R>;
  // class template basic_format_parse_context
  template<class CharT> class basic_format_parse_context;
  using format_parse_context = basic_format_parse_context<char>;
  using wformat_parse_context = basic_format_parse_context<wchar_t>;
  // Formatierung von Bereichen
  // variable template format_kind
  enum class range_format
  {
    disabled,
    map,
    set,
    sequence,
    string,
    debug_string
  };
  template<class R> constexpr /* nicht spezifiziert */ format_kind = /* nicht spezifiziert */;
  template<ranges::input_range R>
    requires same_as<R, remove_cvref_t<R>>
  constexpr range_format format_kind<R> = /* siehe Beschreibung */;
  // Klassentemplate range_formatter
  template<class T, class CharT = char>
    requires same_as<remove_cvref_t<T>, T> && formattable<T, CharT>
  class range_formatter;
  // class template range-default-formatter
  template<range_format K, ranges::input_range R, class CharT>
  struct /*range-default-formatter*/; // exposition-only
  // Spezialisierungen für Maps, Sets und Strings
  template<ranges::input_range R, class CharT>
    requires(format_kind<R> != range_format::deaktiviert) &&
            formattable<ranges::range_reference_t<R>, CharT>
  struct formatter<R, CharT> : /*range-default-formatter*/<format_kind<R>, R, CharT>
  {};
  template<ranges::input_range R>
    requires(format_kind<R> != range_format::deaktiviert)
  constexpr bool enable_nonlocking_formatter_optimization<R> = false;
  // Argumente
  // Klassentemplate basic_format_arg
  template<class Context> class basic_format_arg;
  // Klassentemplate format-arg-store
  template<class Context, class... Args> class /*format-arg-store*/; // exposition-only
  template<class Context = format_context, class... Args>
  /*format-arg-store*/<Context, Args...> make_format_args(Args&... fmt_args);
  template<class... Args>
  /*format-arg-store*/<wformat_context, Args...> make_wformat_args(Args&... args);
  // class format_error
  class format_error;
}

Klassentemplate std::basic_format_string

namespace std {
  template<class CharT, class... Args> struct basic_format_string
  {
  private:
    basic_string_view<CharT> /*str*/; // nur zur Darstellung
  public:
    template<class T> consteval basic_format_string(const T& s);
    basic_format_string(/*runtime-format-string*/<CharT> s) noexcept
      : str(s./*str*/)
    {
    }
    constexpr basic_string_view<CharT> get() const noexcept { return /*str*/; }
  };
}

Konzept std::formattable

template<class T, class CharT>
  concept formattable =
    semiregular<formatter<remove_cvref_t<T>, CharT>> &&
    requires(formatter<remove_cvref_t<T>, CharT> f,
             const formatter<remove_cvref_t<T>, CharT> cf,
             T t,
             basic_format_context<__fmt_iter_for<CharT>, CharT> fc,
             basic_format_parse_context<CharT> pc) {
        { f.parse(pc) } -> same_as<basic_format_parse_context<CharT>::iterator>;
        { cf.format(t, fc) } -> same_as<__fmt_iter_for<CharT>>;
    };

Klassentemplate std::basic_format_parse_context

namespace std {
  template<class CharT> class basic_format_parse_context
  {
  public:
    using char_type = CharT;
    using const_iterator = typename basic_string_view<CharT>::const_iterator;
    using iterator = const_iterator;
  private:
    iterator begin_;     // nur zur Darstellung
    iterator end_;       // nur zur Darstellung
    enum indexing
    {
      unknown,
      manual,
      automatic
    };                   // nur zur Darstellung
    indexing indexing_;  // nur zur Darstellung
    size_t next_arg_id_; // nur zur Darstellung
    size_t num_args_;    // nur zur Darstellung
  public:
    constexpr explicit basic_format_parse_context(basic_string_view<CharT> fmt) noexcept;
    basic_format_parse_context(const basic_format_parse_context&) = delete;
    basic_format_parse_context& operator=(const basic_format_parse_context&) = delete;
    constexpr const_iterator begin() const noexcept;
    constexpr const_iterator end() const noexcept;
    constexpr void advance_to(const_iterator it);
    constexpr size_t next_arg_id();
    constexpr void check_arg_id(size_t id);
    template<class... Ts> constexpr void check_dynamic_spec(size_t id) noexcept;
    constexpr void check_dynamic_spec_integral(size_t id) noexcept;
    constexpr void check_dynamic_spec_string(size_t id) noexcept;
  };
}

Klassentemplate std::basic_format_context

namespace std {
  template<class Out, class CharT> class basic_format_context
  {
    basic_format_args<basic_format_context> args_; // nur zur Darstellung
    Out out_;                                      // nur zur Darstellung
    basic_format_context(const basic_format_context&) = delete;
    basic_format_context& operator=(const basic_format_context&) = delete;
  public:
    using iterator = Out;
    using char_type = CharT;
    template<class T> using formatter_type = formatter<T, CharT>;
    basic_format_arg<basic_format_context> arg(size_t id) const noexcept;
    std::locale locale();
    iterator out();
    void advance_to(iterator it);
  };
}

Variablen-Template std::format_kind

template<ranges::input_range R>
  requires same_as<R, remove_cvref_t<R>>
constexpr range_format format_kind<R> = /* siehe Beschreibung */;

Klassentemplate std::range_formatter

namespace std {
  template<class T, class CharT = char>
    requires same_as<remove_cvref_t<T>, T> && formattable<T, CharT>
  class range_formatter
  {
    formatter<T, CharT> /*underlying_*/; // nur zur Darstellung
    basic_string_view<CharT> /*separator_*/ =
      /*STATICALLY-WIDEN*/<CharT>(", "); // nur zur Darstellung
    basic_string_view<CharT> /*opening-bracket_*/ =
      /*STATICALLY-WIDEN*/<CharT>("[");  // nur zur Darstellung
    basic_string_view<CharT> /*closing-bracket_*/ =
      /*STATICALLY-WIDEN*/<CharT>("]");  // nur zur Darstellung
  public:
    constexpr void set_separator(basic_string_view<CharT> sep) noexcept;
    constexpr void set_brackets(basic_string_view<CharT> opening,
                                basic_string_view<CharT> closing) noexcept;
    constexpr formatter<T, CharT>& underlying() noexcept { return /*underlying_*/; }
    constexpr const formatter<T, CharT>& underlying() const noexcept
    {
      return /*underlying_*/;
    }
    template<class ParseContext>
    constexpr typename ParseContext::iterator parse(ParseContext& ctx);
    template<ranges::input_range R, class FormatContext>
      requires formattable<ranges::range_reference_t<R>, CharT> &&
               same_as<remove_cvref_t<ranges::range_reference_t<R>>, T>
    typename FormatContext::iterator format(R&& r, FormatContext& ctx) const;
  };
}

Klassentemplate __range_default_formatter

namespace std {
  template<ranges::input_range R, class CharT>
  struct /*range-default-formatter*/<range_format::sequence, R, CharT>
  {                                                          // nur zur Darstellung
  private:
    using /*maybe-const-r*/ = /*fmt-maybe-const*/<R, CharT>; // nur zur Darstellung
    range_formatter<remove_cvref_t<ranges::range_reference_t</*maybe-const-r*/>>,
                    CharT>
      /*underlying_*/;                                       // nur zur Darstellung
  public:
    constexpr void set_separator(basic_string_view<CharT> sep) noexcept;
    constexpr void set_brackets(basic_string_view<CharT> opening,
                                basic_string_view<CharT> closing) noexcept;
    template<class ParseContext>
    constexpr typename ParseContext::iterator parse(ParseContext& ctx);
    template<class FormatContext>
    typename FormatContext::iterator format(/*maybe-const-r*/& elems,
                                            FormatContext& ctx) const;
  };
}

Spezialisierung von __range_default_formatter für Maps

namespace std {
  template<ranges::input_range R, class CharT>
  struct /*range-default-formatter*/<range_format::map, R, CharT>
  {
  private:
    using /*maybe-const-map*/ = /*fmt-maybe-const*/<R, CharT>; // nur zur Darstellung
    using /*element-type*/ =                                   // nur zur Darstellung
      remove_cvref_t<ranges::range_reference_t</*maybe-const-map*/>>;
    range_formatter</*element-type*/, CharT> /*underlying_*/;  // nur zur Darstellung
  public:
    constexpr /*range-default-formatter*/();
    template<class ParseContext>
    constexpr typename ParseContext::iterator parse(ParseContext& ctx);
    template<class FormatContext>
    typename FormatContext::iterator format(/*maybe-const-map*/& r,
                                            FormatContext& ctx) const;
  };
}

Spezialisierung von __range_default_formatter für Sets

namespace std {
  template<ranges::input_range R, class CharT>
  struct /*range-default-formatter*/<range_format::set, R, CharT>
  {
  private:
    using /*maybe-const-set*/ = /*fmt-maybe-const*/<R, CharT>; // nur zur Darstellung
    range_formatter<remove_cvref_t<ranges::range_reference_t</*maybe-const-set*/>>,
                    CharT>
      /*underlying_*/;                                         // nur zur Darstellung
  public:
    constexpr /*range-default-formatter*/();
    template<class ParseContext>
    constexpr typename ParseContext::iterator parse(ParseContext& ctx);
    template<class FormatContext>
    typename FormatContext::iterator format(/*maybe-const-set*/& r,
                                            FormatContext& ctx) const;
  };
}

Spezialisierung von __range_default_formatter für Strings

namespace std {
  template<range_format K, ranges::input_range R, class CharT>
    requires(K == range_format::string || K == range_format::debug_string)
  struct /*range-default-formatter*/<K, R, CharT>
  {
  private:
    formatter<basic_string<CharT>, CharT> /*underlying_*/; // exposition-only
  public:
    template<class ParseContext>
    constexpr typename ParseContext::iterator parse(ParseContext& ctx);
    template<class FormatContext>
    typename FormatContext::iterator format(/* siehe Beschreibung */ &str,
                                            FormatContext& ctx) const;
  };
}

Klassentemplate std::basic_format_arg

namespace std {
  template<class Context> class basic_format_arg
  {
  public:
    class handle;
  private:
    using char_type = typename Context::char_type; // nur zur Darstellung
    variant<monostate,
            bool,
            char_type,
            int,
            unsigned int,
            long long int,
            unsigned long long int,
            float,
            double,
            long double,
            const char_type*,
            basic_string_view<char_type>,
            const void*,
            handle>
      value;                                                    // nur zur Darstellung
    template<class T> explicit basic_format_arg(T& v) noexcept; // nur zur Darstellung
  public:
    basic_format_arg() noexcept;
    explicit operator bool() const noexcept;
    template<class Visitor>
    decltype(auto) visit(this basic_format_arg arg, Visitor&& vis);
    template<class R, class Visitor> R visit(this basic_format_arg arg, Visitor&& vis);
  };
}

Klasse std::basic_format_arg::handle

namespace std {
  template<class Context> class basic_format_arg<Context>::handle
  {
    const void* ptr_;                                   // nur zur Darstellung
    void (*format_)(basic_format_parse_context<char_type>&,
                    Context&,
                    const void*);                       // nur zur Darstellung
    template<class T> explicit handle(T& val) noexcept; // nur zur Darstellung
  public:
    void format(basic_format_parse_context<char_type>&, Context& ctx) const;
  };
}

Klassentemplate __format_arg_store

namespace std {
  template<class Context, class... Args> class /*format-arg-store*/
  {                                                             // exposition-only
    array<basic_format_arg<Context>, sizeof...(Args)> /*args*/; // exposition-only
  };
}

Klassentemplate std::basic_format_args

namespace std {
  template<class Context> class basic_format_args
  {
    size_t size_;                           // nur zur Darstellung
    const basic_format_arg<Context>* data_; // nur zur Darstellung
  public:
    template<class... Args>
    basic_format_args(const /*format-arg-store*/<Context, Args...>& store) noexcept;
    basic_format_arg<Context> get(size_t i) const noexcept;
  };
  template<class Context, class... Args>
  basic_format_args(/*format-arg-store*/<Context, Args...>) -> basic_format_args<Context>;
}

Tupel-Formatierer

namespace std {
  template<class CharT, formattable<CharT>... Ts>
  struct formatter</*pair-or-tuple*/<Ts...>, CharT>
  {
  private:
    tuple<formatter<remove_cvref_t<Ts>, CharT>...> /*underlying_*/; // Nur zur Darstellung
    basic_string_view<CharT> /*separator_*/ =
      /*STATICALLY-WIDEN*/<CharT>(", ");                            // Nur zur Darstellung
    basic_string_view<CharT> /*opening-bracket_*/ =
      /*STATICALLY-WIDEN*/<CharT>("(");                             // Nur zur Darstellung
    basic_string_view<CharT> /*closing-bracket_*/ =
      /*STATICALLY-WIDEN*/<CharT>(")");                             // Nur zur Darstellung
  public:
    constexpr void set_separator(basic_string_view<CharT> sep) noexcept;
    constexpr void set_brackets(basic_string_view<CharT> opening,
                                basic_string_view<CharT> closing) noexcept;
    template<class ParseContext>
    constexpr typename ParseContext::iterator parse(ParseContext& ctx);
    template<class FormatContext>
    typename FormatContext::iterator format(/* siehe Beschreibung */ &elems,
                                            FormatContext& ctx) const;
  };
  template<class... Ts>
  constexpr bool enable_nonlocking_formatter_optimization</*pair-or-tuple*/<Ts...>> =
    (enable_nonlocking_formatter_optimization<Ts> && ...);
}

Klasse std::format_error

namespace std {
  class format_error : public runtime_error
  {
  public:
    constexpr explicit format_error(const string& what_arg);
    constexpr explicit format_error(const char* what_arg);
  };
}