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

From cppreference.com
< cpp‎ | header
 
 
Standard Library headers
Note: a slash '/' in a revision mark means that the header was deprecated and/or removed.
Language Support
Concepts
<concepts> (C++20)
Diagnostics
<system_error> (C++11)
General utilities
<bitset>
<tuple> (C++11)
<optional> (C++17)
<any> (C++17)
<variant> (C++17)
<type_traits> (C++11)
<ratio> (C++11)
<chrono> (C++11)
Strings
<charconv> (C++17)
<format> (C++20)

<cwchar>
<cuchar> (C++11)

Localization
<codecvt> (C++11/17)

Containers
<span> (C++20)
<array> (C++11)
<vector>
Iterators
<iterator>
Ranges
<ranges> (C++20)
Algorithms
<execution> (C++17)
Numerics
<bit> (C++20)
<numbers> (C++20)
<cfenv> (C++11)
<cmath>
Input/Output
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/)

Regular expressions
<regex> (C++11)
Filesystem support
<filesystem> (C++17)
Thread support
<thread> (C++11)
<atomic> (C++11)
<mutex> (C++11)
<shared_mutex> (C++14)
<condition_variable> (C++11)  
<future> (C++11)
<stop_token> (C++20)
<semaphore> (C++20)

<latch> (C++20)
<barrier> (C++20)

C compatibility
<cstdbool> (C++11/17/20)
<ccomplex> (C++11/17/20)
<ctgmath> (C++11/17/20)
<cstdalign> (C++11/17/20)
<ciso646> (until C++20)
<stdatomic.h>
 

This header is part of the format library.

Classes

(C++20)
class template that defines formatting rules for a given type
(class template)
formatting string parser state
(class template)
formatting state, including all formatting arguments and the output iterator
(class template)
class template that provides access to a formatting argument for user-defined formatters
(class template)
class that provides access to all formatting arguments
(class template)
exception type thrown on formatting errors
(class)

Functions

(C++20)
stores formatted representation of the arguments in a new string
(function template)
(C++20)
writes out formatted representation of its arguments through an output iterator
(function template)
writes out formatted representation of its arguments through an output iterator, not exceeding specified size
(function template)
determines the number of characters necessary to store the formatted representation of its arguments
(function template)
(C++20)
non-template variant of std::format using type-erased argument representation
(function)
non-template variant of std::format_to using type-erased argument representation
(function template)
argument visitation interface for user-defined formatters
(function template)
creates a type-erased object referencing all formatting arguments, convertible to format_args
(function template)

Synopsis

namespace std {
  // basic format string
  template<class charT, class... Args> struct /*basic-format-string*/; // exposition only
 
  template<class... Args>
    using /*format-string*/ 
    = /*basic-format-string*/<char, type_identity_t<Args>...>; // exposition only
  template<class... Args>
    using /*wformat-string*/ 
    = /*basic-format-string*/<wchar_t, type_identity_t<Args>...>; // exposition only
 
  // 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;
 
  // 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>;
 
  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>;
 
  // arguments
  // class template basic_format_arg
  template<class Context> class basic_format_arg;
 
  template<class Visitor, class Context>
    /* see description */ visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg);
 
  // class template format-arg-store
  template<class Context, class... Args> struct /*format-arg-store*/;  // exposition only
 
  template<class Context = format_context, class... Args>
    /*format-arg-store*/<Context, Args...>
      make_format_args(Args&&... args);
  template<class... Args>
    /*format-arg-store*/<wformat_context, Args...>
      make_wformat_args(Args&&... args);
 
  // 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 format_error
  class format_error;
}

Class template basic-format-string

namespace std {
  template<class charT, class... Args>
  struct /*basic-format-string*/ {  // exposition only
  private:
    basic_string_view<charT> str_;  // exposition only
 
  public:
    template<class T> consteval /*basic-format-string*/(const T& s);
  };
}

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

Class template 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
 
  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;
    std::locale locale();
 
    iterator out();
    void advance_to(iterator it);
  };
}

Class template 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(const T* p) noexcept;                  // exposition only
 
  public:
    basic_format_arg() noexcept;
 
    explicit operator bool() const noexcept;
  };
}

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

Class template format-arg-store

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

Class template 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:
    basic_format_args() noexcept;
 
    template<class... Args>
      basic_format_args(const /*format-arg-store*/<Context, Args...>& store) noexcept;
 
    basic_format_arg<Context> get(size_t i) const noexcept;
  };
}

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