format_tuple_formatter

format_tuple_formatter (튜플 포맷터)

std::formatterstd::pairstd::tuple에 대한 템플릿 특수화를 설명하는 페이지예요. 이 특수화를 사용하면 서식 지정 함수를 통해 pair나 tuple을 요소들의 모음으로 된 텍스트 표현으로 변환할 수 있어요.

출처: cppreference

본문

개요

<format> 헤더에 정의되어 있어요:

template < class CharT , std :: formattable < CharT > ... Ts >
struct formatter < /*pair-or-tuple*/ < Ts ... > , CharT > ;

(C++23부터 사용 가능)

노출 전용 이름 /*pair-or-tuple*/은 클래스 템플릿 std::pair 또는 std::tuple 중 하나를 나타내요.

이 특수화는 ( std :: formattable < const Ts , CharT > && ... )가 참이면 Formatter 요구 사항을 충족해요. 항상 BasicFormatter 요구 사항도 충족해요.

형식 지정 (Format specification)

tuple-format-spec의 문법은 다음과 같아요:

tuple-fill-and-align (선택) width (선택) tuple-type (선택)

tuple-fill-and-alignfill-and-align과 동일하게 해석되지만, tuple-fill-and-align의 fill 문자는 {, }, : 외의 문자만 가능해요.

width는 표준 형식 width 지정 방식에 설명되어 있어요.

tuple-type은 튜플의 형식 지정 방식을 변경하며, 특정 옵션은 특정 인자 타입에서만 유효해요.

사용 가능한 튜플 표현 타입은 다음과 같아요:

  • m: 여는 괄호와 닫는 괄호를 모두 ""로, 구분자는 ": "로 사용해요. m이 tuple-type으로 선택되면 sizeof ...( Ts ) == 2가 참이 아닌 경우 프로그램이 ill-formed예요.
  • n: 구분자, 여는 괄호, 닫는 괄호를 모두 ""로 사용해요.

멤버 객체

멤버 이름 정의
underlying_ (비공개) std :: tuple < std :: formatter < std :: remove_cvref_t < Ts > , CharT > ... > 타입의 내부 포맷터 튜플 (노출 전용 멤버 객체*)
separator_ (비공개) 튜플 형식 지정 결과의 구분자를 나타내는 문자열 (기본값 ", ") (노출 전용 멤버 객체*)
opening-bracket_ (비공개) 튜플 형식 지정 결과의 여는 괄호를 나타내는 문자열 (기본값 "(") (노출 전용 멤버 객체*)
closing-bracket_ (비공개) 튜플 형식 지정 결과의 닫는 괄호를 나타내는 문자열 (기본값 ")") (노출 전용 멤버 객체*)

멤버 함수

함수 설명
set_separator 튜플 형식 지정 결과의 구분자를 지정해요 (공개 멤버 함수)
set_brackets 튜플 형식 지정 결과의 여는 괄호와 닫는 괄호를 지정해요 (공개 멤버 함수)
parse tuple-format-spec에 따라 형식 지정자를 구문 분석해요 (공개 멤버 함수)
format tuple-format-spec에 따라 튜플 형식 지정 출력을 작성해요 (공개 멤버 함수)

std::formatter< pair-or-tuple >:: set_separator

constexpr void set_separator ( std :: basic_string_view < CharT > sep ) noexcept ;

sepseparator_에 할당해요.

std::formatter< pair-or-tuple >:: set_brackets

constexpr void set_brackets ( std :: basic_string_view < CharT > opening , std :: basic_string_view < CharT > closing ) noexcept ;

openingclosing을 각각 opening-bracket_closing-bracket_에 할당해요.

std::formatter< pair-or-tuple >:: parse

template < class ParseContext >
constexpr auto parse ( ParseContext & ctx ) -> ParseContext :: iterator ;

형식 지정자를 tuple-format-spec으로 구문 분석하고, 구문 분석된 지정자를 현재 객체에 저장해요.

tuple-type 또는 n 옵션이 있으면 opening-bracket, closing-bracket, separator의 값이 필요에 따라 수정돼요.

underlying_의 각 요소 e에 대해 e . parse ( ctx )를 호출하여 빈 format-spec을 구문 분석하고, e . set_debug_format ()이 유효한 표현식이면 e . set_debug_format ()을 호출해요.

tuple-format-spec의 끝을 지난 반복자를 반환해요.

std::formatter< pair-or-tuple >:: format

template < class FormatContext >
FormatContext :: iterator format ( /*maybe-const-pair-or-tuple*/ < Ts ... >& elems , FormatContext & ctx ) const ;

/*maybe-const-pair-or-tuple*/은 다음을 나타내요:

  • ( std :: formattable < const Ts , CharT > && ... )가 참이면 const /*pair-or-tuple*/,
  • 그렇지 않으면 /*pair-or-tuple*/.

tuple-format-spec에 따라 다음을 순서대로 ctx . out ()에 작성해요:

  • opening-bracket_,
  • 각 인덱스 I에 대해 [ 0 , sizeof ...( Ts ) ) 범위에서:
    • I != 0이면 separator_, std :: get < I > ( underlying_ )을 통한 std :: get < I > ( elems ) 작성 결과,
  • closing-bracket_.

출력 범위의 끝을 지난 반복자를 반환해요.

결함 보고서 (Defect reports)

다음 동작 변경 결함 보고서는 이전에 발표된 C++ 표준에 소급 적용되었어요.

DR 적용 대상 발표된 동작 올바른 동작
LWG 3892 C++23 중첩 튜플의 형식 지정이 올바르지 않았음 수정됨

같이 보기 (See also)

formatter (C++20) 주어진 타입에 대한 형식 지정 규칙을 정의해요 (클래스 템플릿)

더 알아보기 (Learn more)

cppreference