Namespaces
Variants

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

From cppreference.net
Standard library headers

Cet en-tête fait partie de la bibliothèque de traitement de texte .

Table des matières

Concepts

spécifie qu'un type est formatable, c'est-à-dire qu'il spécialise std::formatter et fournit les fonctions membres parse et format
(concept)

Énumérations

spécifie comment un range doit être formaté
(enum)

Classes

(C++20)
définit les règles de formatage pour un type donné
(modèle de classe)
classe template qui aide à implémenter std::formatter les spécialisations pour les types de plage
(classe template)
état d'analyse de chaîne de formatage
(modèle de classe)
état de formatage, incluant tous les arguments de formatage et l'itérateur de sortie
(modèle de classe)
classe template qui fournit l'accès à un argument de formatage pour les formateurs personnalisés
(classe template)
classe qui fournit l'accès à tous les arguments de formatage
(modèle de classe)
modèle de classe qui effectue des vérifications de chaîne de format au moment de la compilation lors de la construction
(modèle de classe)
type d'exception levée lors d'erreurs de formatage
(classe)
Spécialisations de formateur
prise en charge du formatage pour pair et tuple
(spécialisation de modèle de classe)
prise en charge du formatage pour les plages
(spécialisation de modèle de classe)

Fonctions

(C++20)
stocke la représentation formatée des arguments dans une nouvelle chaîne
(modèle de fonction)
(C++20)
écrit une représentation formatée de ses arguments via un itérateur de sortie
(modèle de fonction)
écrit une représentation formatée de ses arguments via un itérateur de sortie, sans dépasser la taille spécifiée
(modèle de fonction)
détermine le nombre de caractères nécessaires pour stocker la représentation formatée de ses arguments
(modèle de fonction)
crée des chaînes de formatage à l'exécution directement utilisables dans les fonctions de formatage orientées utilisateur
(fonction)
(C++20)
variante non template de std::format utilisant une représentation d'arguments à effacement de type
(fonction)
(C++20)
variante non template de std::format_to utilisant une représentation d'arguments avec effacement de type
(fonction template)
(C++20) (obsolète en C++26)
interface de visite d'argument pour les formateurs personnalisés
(modèle de fonction)
crée un objet à effacement de type référençant tous les arguments de formatage, convertible en format_args
(modèle de fonction)

Aides

sélectionne un std::range_format approprié pour un intervalle
(modèle de variable)
indique que le type d'argument peut être imprimé efficacement
(modèle de variable)

Synopsis

namespace std {
  // modèle de classe basic_format_context
  template<class Out, class CharT> class basic_format_context;
  using format_context = basic_format_context</* non spécifié */, char>;
  using wformat_context = basic_format_context</* non spécifié */, wchar_t>;
  // modèle de classe 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>;
  // modèle de classe basic_format_string
  template<class CharT, class... Args> struct basic_format_string;
  template<class CharT> struct /*runtime-format-string*/
  {                                   // exposition uniquement
  private:
    basic_string_view<CharT> /*str*/; // exposition uniquement
  public:
    /*chaîne-de-formatage-exécution*/(basic_string_view<CharT> s) noexcept
      : /*str*/(s)
    {
    }
    /*chaîne-de-formatage-exécution*/(const /*chaîne-de-formatage-exécution*/&) = delete;
    /*chaîne de format d'exécution*/& operator=(const /*chaîne de format d'exécution*/&) = delete;
  };
  /*chaîne-de-formatage-exécution*/<char> runtime_format(string_view fmt) noexcept { return fmt; }
  /*chaîne-de-formatage-exécution*/<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>...>;
  // fonctions de formatage
  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);
  // formateur
  template<class T, class CharT = char> struct formatter;
  // verrouillage du formateur
  template<class T> constexpr bool enable_nonlocking_formatter_optimization = false;
  // concept formattable
  template<class T, class CharT>
  concept formattable = /* voir description */;
  template<class R, class CharT>
  concept /*const-formattable-range*/ = // exposition uniquement
    ranges::input_range<const R> &&
    formattable<ranges::range_reference_t<const R>, CharT>;
  template<class R, class CharT>
  using /*fmt-maybe-const*/ =           // exposition uniquement
    conditional_t</*const-formattable-range*/<R, CharT>, const R, R>;
  // modèle de classe 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>;
  // formatage des plages
  // variable template format_kind
  enum class range_format
  {
    disabled,
    map,
    set,
    sequence,
    string,
    debug_string
  };
  template<class R> constexpr /* non spécifié */ format_kind = /* non spécifié */;
  template<ranges::input_range R>
    requires same_as<R, remove_cvref_t<R>>
  constexpr range_format format_kind<R> = /* voir description */;
  // modèle de classe range_formatter
  template<class T, class CharT = char>
    requires same_as<remove_cvref_t<T>, T> && formattable<T, CharT>
  class range_formatter;
  // modèle de classe range-default-formatter
  template<range_format K, ranges::input_range R, class CharT>
  struct /*range-default-formatter*/; // exposition uniquement
  // spécialisations pour les maps, les sets et les strings
  template<ranges::input_range R, class CharT>
    requires(format_kind<R> != range_format::désactivé) &&
            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::désactivé)
  constexpr bool enable_nonlocking_formatter_optimization<R> = false;
  // arguments
  // modèle de classe basic_format_arg
  template<class Context> class basic_format_arg;
  // modèle de classe format-arg-store
  template<class Context, class... Args> class /*format-arg-store*/; // exposition uniquement
  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;
}

Modèle de classe std::basic_format_string

namespace std {
  template<class CharT, class... Args> struct basic_format_string
  {
  private:
    basic_string_view<CharT> /*str*/; // exposition uniquement
  public:
    template<class T> consteval basic_format_string(const T& s);
    basic_format_string(/*chaîne de format d'exécution*/<CharT> s) noexcept
      : str(s./*str*/)
    {
    }
    constexpr basic_string_view<CharT> get() const noexcept { return /*str*/; }
  };
}

Concept 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>>;
    };

Modèle de classe 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_;     // exposition uniquement
    iterator end_;       // exposition uniquement
    enum indexing
    {
      unknown,
      manual,
      automatic
    };                   // exposition uniquement
    indexing indexing_;  // exposition uniquement
    size_t next_arg_id_; // exposition uniquement
    size_t num_args_;    // exposition uniquement
  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;
  };
}

Modèle de classe std::basic_format_context

namespace std {
  template<class Out, class CharT> class basic_format_context
  {
    basic_format_args<basic_format_context> args_; // exposition uniquement
    Out out_;                                      // exposition uniquement
    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);
  };
}

Modèle de variable std::format_kind

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

Modèle de classe 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_*/; // exposition uniquement
    basic_string_view<CharT> /*separator_*/ =
      /*STATICALLY-WIDEN*/<CharT>(", "); // exposition uniquement
    basic_string_view<CharT> /*opening-bracket_*/ =
      /*STATICALLY-WIDEN*/<CharT>("[");  // exposition uniquement
    basic_string_view<CharT> /*closing-bracket_*/ =
      /*STATICALLY-WIDEN*/<CharT>("]");  // exposition uniquement
  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;
  };
}

Modèle de classe __range_default_formatter

namespace std {
  template<ranges::input_range R, class CharT>
  struct /*range-default-formatter*/<range_format::sequence, R, CharT>
  {                                                          // exposition-only
  private:
    using /*maybe-const-r*/ = /*fmt-maybe-const*/<R, CharT>; // exposition-only
    range_formatter<remove_cvref_t<ranges::range_reference_t</*maybe-const-r*/>>,
                    CharT>
      /*underlying_*/;                                       // exposition-only
  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;
  };
}

Spécialisation de __range_default_formatter pour les 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>; // exposition uniquement
    using /*element-type*/ =                                   // exposition uniquement
      remove_cvref_t<ranges::range_reference_t</*maybe-const-map*/>>;
    range_formatter</*element-type*/, CharT> /*underlying_*/;  // exposition uniquement
  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;
  };
}

Spécialisation de __range_default_formatter pour les ensembles

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>; // exposition uniquement
    range_formatter<remove_cvref_t<ranges::range_reference_t</*maybe-const-set*/>>,
                    CharT>
      /*underlying_*/;                                         // exposition uniquement
  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;
  };
}

Spécialisation de __range_default_formatter pour les chaînes de caractères

namespace std {
  template<range_format K, ranges::input_range R, class CharT>
    requires(K == range_format::string || K == range_format::debug_string)
  struct /*formateur-par-défaut-de-plage*/<K, R, CharT>
  {
  private:
    formatter<basic_string<CharT>, CharT> /*sous-jacent_*/; // exposition uniquement
  public:
    template<class ParseContext>
    constexpr typename ParseContext::iterator parse(ParseContext& ctx);
    template<class FormatContext>
    typename FormatContext::iterator format(/* voir description */ &str,
                                            FormatContext& ctx) const;
  };
}

Modèle de classe std::basic_format_arg

namespace std {
  template<class Context> class basic_format_arg
  {
  public:
    class handle;
  private:
    using char_type = typename Context::char_type; // exposition uniquement
    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;                                                    // exposition uniquement
    template<class T> explicit basic_format_arg(T& v) noexcept; // exposition uniquement
  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);
  };
}

Classe std::basic_format_arg::handle

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

Modèle de classe __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
  };
}

Modèle de classe std::basic_format_args

namespace std {
  template<class Context> class basic_format_args
  {
    size_t size_;                           // exposition uniquement
    const basic_format_arg<Context>* data_; // exposition uniquement
  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>;
}

Formateur de tuple

namespace std {
  template<class CharT, formattable<CharT>... Ts>
  struct formatter</*pair-or-tuple*/<Ts...>, CharT>
  {
  private:
    tuple<formatter<remove_cvref_t<Ts>, CharT>...> /*underlying_*/; // exposition uniquement
    basic_string_view<CharT> /*separator_*/ =
      /*STATICALLY-WIDEN*/<CharT>(", ");                            // exposition uniquement
    basic_string_view<CharT> /*opening-bracket_*/ =
      /*STATICALLY-WIDEN*/<CharT>("(");                             // exposition uniquement
    basic_string_view<CharT> /*closing-bracket_*/ =
      /*STATICALLY-WIDEN*/<CharT>(")");                             // exposition uniquement
  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(/* voir description */ &elems,
                                            FormatContext& ctx) const;
  };
  template<class... Ts>
  constexpr bool enable_nonlocking_formatter_optimization</*pair-or-tuple*/<Ts...>> =
    (enable_nonlocking_formatter_optimization<Ts> && ...);
}

Classe 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);
  };
}
**Note:** Le code C++ n'a pas été traduit car il se trouve dans des balises `
` et contient des termes spécifiques au C++ qui doivent être préservés selon les instructions. Seul le texte environnant (s'il y en avait) aurait été traduit en français.