Namespaces
Variants

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

From cppreference.net
Standard library headers

이 헤더는 텍스트 처리 라이브러리의 일부입니다.

**변역 설명:** - "Contents" → "목차"로 번역 - C++ 관련 용어(Concepts, Enumerations, Classes, Functions, Helpers, Synopsis 등)는 원문 유지 - HTML 태그와 속성은 그대로 보존 - ` `, ` ` 태그 내의 C++ 코드(std::basic_format_string 등)는 번역하지 않음 - 일반 텍스트만 한국어로 정확하게 번역

목차

Concepts

특정 타입이 포맷 가능함을 명시합니다. 즉, std::formatter 를 특수화하고 parse format 멤버 함수를 제공함을 의미합니다.
(concept)

열거형

범위가 어떻게 포맷되어야 하는지 지정합니다
(enum)

클래스

(C++20)
주어진 타입에 대한 서식 규칙을 정의함
(클래스 템플릿)
범위 타입에 대한 std::formatter 특수화를 구현하는 데 도움을 주는 클래스 템플릿
(클래스 템플릿)
포맷팅 문자열 파서 상태
(클래스 템플릿)
포맷팅 상태, 모든 포맷팅 인자와 출력 반복자를 포함함
(클래스 템플릿)
사용자 정의 포매터를 위한 포매팅 인자에 대한 접근을 제공하는 클래스 템플릿
(클래스 템플릿)
모든 포맷팅 인수에 접근을 제공하는 클래스
(클래스 템플릿)
생성 시점에 컴파일 타임 형식 문자열 검사를 수행하는 클래스 템플릿
(클래스 템플릿)
포맷팅 오류 시 발생하는 예외 타입
(클래스)
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 {
  // 클래스 템플릿 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> /*str*/; // exposition-only
  public:
    /*런타임-형식-문자열*/(basic_string_view<CharT> s) noexcept
      : /*str*/(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>...>;
  // 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);
  // 포매터
  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>;
  // 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>;
  // 범위의 서식 지정
  // 변수 템플릿 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;
  // class template 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::비활성화됨) &&
            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::비활성화됨)
  constexpr bool enable_nonlocking_formatter_optimization<R> = false;
  // arguments
  // 클래스 템플릿 basic_format_arg
  template<class Context> class basic_format_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*/; // 설명 전용
  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 의 set 전문화

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