Namespaces
Variants

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

From cppreference.net
Standard library headers

此头文件是 文本处理 库的组成部分。

目录

概念

指定类型是可格式化的,即它特化了 std::formatter 并提供了成员函数 parse format
(概念)

枚举类型

指定范围的格式化方式
(枚举类型)

(C++20)
定义特定类型的格式化规则
(类模板)
用于帮助实现范围类型的 std::formatter 特化的类模板
(类模板)
格式化字符串解析器状态
(类模板)
格式化状态,包含所有格式化参数和输出迭代器
(类模板)
为自定义格式化器提供格式化参数访问的类模板
(类模板)
提供对所有格式化参数访问的类
(类模板)
在构造时执行编译期格式字符串检查的类模板
(类模板)
格式化错误时抛出的异常类型
(类)
格式化器特化
pair tuple 的格式化支持
(类模板特化)
范围类型的格式化支持
(类模板特化)

函数

(C++20)
将参数的格式化表示存储到新字符串中
(函数模板)
(C++20)
通过输出迭代器写入其参数的格式化表示
(函数模板)
通过输出迭代器写入其参数的格式化表示,不超过指定大小
(函数模板)
确定存储其参数的格式化表示所需的字符数量
(函数模板)
创建可直接用于面向用户的格式化函数的运行时格式字符串
(函数)
(C++20)
std::format 的非模板变体,使用类型擦除的参数表示
(函数)
(C++20)
使用类型擦除参数表示的 std::format_to 的非模板变体
(函数模板)
(C++20) (deprecated in C++26)
用户自定义格式化器的参数访问接口
(函数模板)
创建类型擦除对象,引用所有格式化参数,可转换为 format_args
(函数模板)

辅助函数

为范围选择适宜的 std::range_format
(变量模板)
指示参数类型可以被高效打印
(变量模板)

概要

namespace std {
  // class template basic_format_context
  template<class Out, class CharT> class basic_format_context;
  using format_context = basic_format_context</* 未指定 */, char>;
  using wformat_context = basic_format_context</* 未指定 */, wchar_t>;
  // 类模板 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>;
  // 类模板 basic_format_string
  template<class CharT, class... Args> struct basic_format_string;
  template<class CharT> struct /*运行时格式字符串*/
  {                                   // exposition-only
  private:
    basic_string_view<CharT> /*字符串*/; // exposition-only
  public:
    /*运行时格式字符串*/(basic_string_view<CharT> s) noexcept
      : /*字符串*/(s)
    {
    }
    /*运行时格式字符串*/(const /*运行时格式字符串*/&) = delete;
    /*运行时格式字符串*/& operator=(const /*运行时格式字符串*/&) = delete;
  };
  /*运行时格式字符串*/<char> runtime_format(string_view fmt) noexcept { return fmt; }
  /*运行时格式字符串*/<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>...>;
  // 格式化函数
  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;
  // 格式化器锁定
  template<class T> constexpr bool enable_nonlocking_formatter_optimization = false;
  // concept formattable
  template<class T, class CharT>
  concept formattable = /* 查看描述 */;
  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>;
  // 类模板 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>;
  // 范围的格式化
  // variable template format_kind
  enum class range_format
  {
    disabled,
    map,
    set,
    sequence,
    string,
    debug_string
  };
  template<class R> constexpr /* 未指定 */ format_kind = /* 未指定 */;
  template<ranges::input_range R>
    requires same_as<R, remove_cvref_t<R>>
  constexpr range_format format_kind<R> = /* 查看描述 */;
  // class template range_formatter
  template<class T, class CharT = char>
    requires same_as<remove_cvref_t<T>, T> && formattable<T, CharT>
  class range_formatter;
  // 类模板 range-default-formatter
  template<range_format K, ranges::input_range R, class CharT>
  struct /*range-default-formatter*/; // exposition-only
  // 针对映射、集合和字符串的特化
  template<ranges::input_range R, class CharT>
    requires(format_kind<R> != range_format::disabled) &&
            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::disabled)
  constexpr bool enable_nonlocking_formatter_optimization<R> = false;
  // arguments
  // 类模板 basic_format_arg
  template<class Context> class basic_format_arg;
  // 类模板 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;
}

类模板 std::basic_format_string

namespace std {
  template<class CharT, class... Args> struct basic_format_string
  {
  private:
    basic_string_view<CharT> /*str*/; // 仅用于说明
  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*/; }
  };
}

概念 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>>;
    };

类模板 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_;     // 仅用于说明
    iterator end_;       // 仅用于说明
    enum indexing
    {
      unknown,
      manual,
      automatic
    };                   // 仅用于说明
    indexing indexing_;  // 仅用于说明
    size_t next_arg_id_; // 仅用于说明
    size_t num_args_;    // 仅用于说明
  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;
  };
}

类模板 std::basic_format_context

namespace std {
  template<class Out, class CharT> class basic_format_context
  {
    basic_format_args<basic_format_context> args_; // 仅用于说明
    Out out_;                                      // 仅用于说明
    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);
  };
}

变量模板 std::format_kind

template<ranges::input_range R>
  requires same_as<R, remove_cvref_t<R>>
constexpr range_format format_kind<R> = /* 详见描述 */;

类模板 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_*/; // 仅用于说明
    basic_string_view<CharT> /*separator_*/ =
      /*STATICALLY-WIDEN*/<CharT>(", "); // 仅用于说明
    basic_string_view<CharT> /*opening-bracket_*/ =
      /*STATICALLY-WIDEN*/<CharT>("[");  // 仅用于说明
    basic_string_view<CharT> /*closing-bracket_*/ =
      /*STATICALLY-WIDEN*/<CharT>("]");  // 仅用于说明
  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;
  };
}

类模板 __range_default_formatter

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

__range_default_formatter 针对映射容器的特化版本

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>; // 仅用于说明
    using /*element-type*/ =                                   // 仅用于说明
      remove_cvref_t<ranges::range_reference_t</*maybe-const-map*/>>;
    range_formatter</*element-type*/, CharT> /*underlying_*/;  // 仅用于说明
  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;
  };
}

__range_default_formatter 针对集合的模板特化

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>; // 仅用于说明
    range_formatter<remove_cvref_t<ranges::range_reference_t</*maybe-const-set*/>>,
                    CharT>
      /*underlying_*/;                                         // 仅用于说明
  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;
  };
}

__range_default_formatter 针对字符串的特化版本

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_*/; // 仅用于说明
  public:
    template<class ParseContext>
    constexpr typename ParseContext::iterator parse(ParseContext& ctx);
    template<class FormatContext>
    typename FormatContext::iterator format(/* 参见描述 */ &str,
                                            FormatContext& ctx) const;
  };
}

类模板 std::basic_format_arg

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

std::basic_format_arg::handle

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

类模板 __format_arg_store

namespace std {
  template<class Context, class... Args> class /*format-arg-store*/
  {                                                             // 仅用于说明
    array<basic_format_arg<Context>, sizeof...(Args)> /*args*/; // 仅用于说明
  };
}

类模板 std::basic_format_args

namespace std {
  template<class Context> class basic_format_args
  {
    size_t size_;                           // 仅用于说明
    const basic_format_arg<Context>* data_; // 仅用于说明
  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>;
}

元组格式化器

namespace std {
  template<class CharT, formattable<CharT>... Ts>
  struct formatter</*pair-or-tuple*/<Ts...>, CharT>
  {
  private:
    tuple<formatter<remove_cvref_t<Ts>, CharT>...> /*underlying_*/; // 仅用于说明
    basic_string_view<CharT> /*separator_*/ =
      /*STATICALLY-WIDEN*/<CharT>(", ");                            // 仅用于说明
    basic_string_view<CharT> /*opening-bracket_*/ =
      /*STATICALLY-WIDEN*/<CharT>("(");                             // 仅用于说明
    basic_string_view<CharT> /*closing-bracket_*/ =
      /*STATICALLY-WIDEN*/<CharT>(")");                             // 仅用于说明
  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(/* 参见描述 */ &elems,
                                            FormatContext& ctx) const;
  };
  template<class... Ts>
  constexpr bool enable_nonlocking_formatter_optimization</*pair-or-tuple*/<Ts...>> =
    (enable_nonlocking_formatter_optimization<Ts> && ...);
}

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