名稱空間
變體
操作

標準庫標頭檔案 <format> (C++20)

來自 cppreference.com
 
 
標準庫標頭檔案
通用工具
<any> (C++17)
<bitset>
<bit> (C++20)
<charconv> (C++17)
<expected> (C++23)
<format> (C++20)
<functional>
<optional> (C++17)
<tuple> (C++11)
<typeindex> (C++11)
<utility>
<variant> (C++17)
容器
<array> (C++11)
<deque>
<flat_map> (C++23)
<flat_set> (C++23)
<forward_list> (C++11)
<hive> (C++26)
<inplace_vector> (C++26)   
<list>
<map>
<mdspan> (C++23)
<queue>
<set>
<span> (C++20)
<stack>
<unordered_map> (C++11)
<unordered_set> (C++11)
<vector>
迭代器
<iterator>
Ranges
<generator> (C++23)
<ranges> (C++20)
演算法
<algorithm>
<numeric>
字串
<cctype>
<cstring>
<cuchar> (C++11)
<cwchar>
<cwctype>
<string_view> (C++17)
<string>
文字處理
<clocale>
<codecvt> (C++11/17/26*)
<locale>
<regex> (C++11)
<text_encoding> (C++26)   
數值
<cfenv> (C++11)
<cmath>
<complex>
<linalg> (C++26)
<numbers> (C++20)
<random> (C++11)
<simd> (C++26)
<valarray>
時間
<chrono> (C++11)
<ctime>
C 相容性
<ccomplex> (C++11/17/20*)
<ciso646> (直到 C++20)
<cstdalign> (C++11/17/20*)
<cstdbool> (C++11/17/20*)
<ctgmath> (C++11/17/20*)
 

此標頭檔案是文字處理庫的一部分。

目錄

概念

指定一個型別是可格式化的,即它特化了 std::formatter 並提供了成員函式 parseformat
(概念) [編輯]

列舉

指定範圍應如何格式化
(列舉) [編輯]

(C++20)
定義給定型別的格式化規則
(類模板) [編輯]
有助於實現範圍型別的 std::formatter 特化的類模板
(類模板) [編輯]
格式化字串解析器狀態
(類模板) [編輯]
格式化狀態,包括所有格式化引數和輸出迭代器
(類模板) [編輯]
為使用者定義格式化器提供格式化引數訪問的類模板
(類模板) [編輯]
提供對所有格式化引數訪問的類
(類模板) [編輯]
在構造時執行編譯時格式字串檢查的類模板
(類模板) [編輯]
格式化錯誤時丟擲的異常型別
(類) [編輯]
格式化器特化
pairtuple 的格式化支援
(類模板特化) [編輯]
對範圍的格式化支援
(類模板特化) [編輯]

函式

(C++20)
將引數的格式化表示儲存在新字串中
(函式模板) [編輯]
(C++20)
透過輸出迭代器寫出其引數的格式化表示
(函式模板) [編輯]
透過輸出迭代器寫出其引數的格式化表示,不超過指定大小
(函式模板) [編輯]
確定儲存其引數格式化表示所需的字元數
(函式模板) [編輯]
建立可直接用於面向使用者的格式化函式的執行時格式字串
(函式) [編輯]
(C++20)
使用型別擦除引數表示的 std::format 的非模板變體
(函式) [編輯]
使用型別擦除引數表示的 std::format_to 的非模板變體
(函式模板) [編輯]
(C++20) (在 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</* unspecified */, char>;
  using wformat_context = basic_format_context</* unspecified */, wchar_t>;
 
  // class template 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>;
 
  // class template basic_format_string
  template<class CharT, class... Args>
    struct basic_format_string;
 
  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>...>;
 
  // formatting functions
  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;
 
  // variable template enable_nonlocking_formatter_optimization
  template<class T>
    constexpr bool enable_nonlocking_formatter_optimization = false;
 
  // concept formattable
  template<class T, class CharT>
    concept formattable = /* see description */;
 
  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>;
 
  // formatting of ranges
  // variable template format_kind
  enum class range_format {
    disabled,
    map,
    set,
    sequence,
    string,
    debug_string
  };
 
  template<class R>
    constexpr /* unspecified */ format_kind = /* unspecified */;
 
  template<ranges::input_range R>
      requires same_as<R, remove_cvref_t<R>>
    constexpr range_format format_kind<R> = /* see description */;
 
  // class template 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
 
  // specializations for maps, sets, and strings
  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> { };
 
  // arguments
  // class template basic_format_arg
  template<class Context> class basic_format_arg;
 
  template<class Visitor, class Context>
    decltype(auto) visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg);
 
  // class template 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;         // exposition only
 
  public:
    template<class T> consteval basic_format_string(const T& s);
 
    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_;                                    // exposition only
    iterator end_;                                      // exposition only
    enum indexing { unknown, manual, automatic };       // exposition only
    indexing indexing_;                                 // exposition only
    size_t next_arg_id_;                                // exposition only
    size_t num_args_;                                   // exposition only
 
  public:
    constexpr explicit basic_format_parse_context(basic_string_view<CharT> fmt,
                                                  size_t num_args = 0) 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_;      // exposition only
    Out out_;                                           // exposition only
 
    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> = /* see description */;

[編輯] 類模板 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 only
    basic_string_view<CharT> separator_ =                   // exposition only
      __STATICALLY_WIDEN<CharT>(", ");
    basic_string_view<CharT> __opening_bracket_ =           // exposition only
      __STATICALLY_WIDEN<CharT>("[");
    basic_string_view<CharT> __closing_bracket_ =           // exposition only
      __STATICALLY_WIDEN<CharT>("]");
 
  public:
    constexpr void set_separator(basic_string_view<CharT> sep);
    constexpr void set_brackets(basic_string_view<CharT> opening,
                                basic_string_view<CharT> closing);
    constexpr formatter<T, CharT>& underlying() { return underlying_; }
    constexpr const formatter<T, CharT>& underlying() const { 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_;       // exposition only
 
  public:
    constexpr void set_separator(basic_string_view<CharT> sep);
    constexpr void set_brackets(basic_string_view<CharT> opening,
                                basic_string_view<CharT> closing);
 
    template<class ParseContext>
      constexpr typename ParseContext::iterator
        parse(ParseContext& ctx);
 
    template<class FormatContext>
      typename FormatContext::iterator
        format(__maybe_const_r& elems, FormatContext& ctx) const;
  };
}

[編輯] 針對 map 的 __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>;          // exposition only
    using __element_type =                                          // exposition only
      remove_cvref_t<ranges::range_reference_t<__maybe_const_map>>;
    range_formatter<__element_type, CharT> underlying_;             // exposition only
  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;
  };
}

[編輯] 針對 set 的 __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>;          // exposition only
    range_formatter<remove_cvref_t<ranges::range_reference_t<__maybe_const_set>>,
                    CharT> underlying_;                             // exposition only
  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;
  };
}

[編輯] 針對 string 的 __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_;              // exposition only
 
  public:
    template<class ParseContext>
      constexpr typename ParseContext::iterator
        parse(ParseContext& ctx);
 
    template<class FormatContext>
      typename FormatContext::iterator
        format(/* see description */& 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;                     // exposition only
 
    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 only
 
    template<class T> explicit basic_format_arg(T&& v) noexcept;       // exposition only
    explicit basic_format_arg(float n) noexcept;                       // exposition only
    explicit basic_format_arg(double n) noexcept;                      // exposition only
    explicit basic_format_arg(long double n) noexcept;                 // exposition only
    explicit basic_format_arg(const char_type* s);                     // exposition only
 
    template<class traits>
      explicit basic_format_arg(
        basic_string_view<char_type, traits> s) noexcept;              // exposition only
 
    template<class traits, class Allocator>
      explicit basic_format_arg(
        const basic_string<char_type, traits, Allocator>& s) noexcept; // exposition only
 
    explicit basic_format_arg(nullptr_t) noexcept;                     // exposition only
 
    template<class T>
      explicit basic_format_arg(T* p) noexcept;                        // exposition only
 
  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_;                                           // exposition only
    void (*format_)(basic_format_parse_context<char_type>&,
                    Context&, const void*);                     // exposition only
 
    template<class T> explicit handle(T&& val) noexcept;        // exposition only
 
    friend class basic_format_arg<Context>;                     // exposition only
 
  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 {                                    // exposition only
    array<basic_format_arg<Context>, sizeof...(Args)> args;     // exposition only
  };
}

[編輯] 類模板 std::basic_format_args

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

[編輯] 元組格式化器

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 only
    basic_string_view<CharT> separator_ =                           // exposition only
      __STATICALLY_WIDEN<CharT>(", ");
    basic_string_view<CharT> opening_bracket_ =                     // exposition only
      __STATICALLY_WIDEN<CharT>("(");
    basic_string_view<CharT> closing_bracket_ =                     // exposition only
      __STATICALLY_WIDEN<CharT>(")");
 
  public:
    constexpr void set_separator(basic_string_view<CharT> sep);
    constexpr void set_brackets(basic_string_view<CharT> opening,
                                basic_string_view<CharT> closing);
 
    template<class ParseContext>
      constexpr typename ParseContext::iterator
        parse(ParseContext& ctx);
 
    template<class FormatContext>
      typename FormatContext::iterator
        format(/* see description */& elems, FormatContext& ctx) const;
  };
}

[編輯] std::format_error

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