Namespaces
Variants

std:: format_to

From cppreference.net
헤더 파일에 정의됨 <format>
template < class OutputIt, class ... Args >

OutputIt format_to ( OutputIt out,

std:: format_string < Args... > fmt, Args && ... args ) ;
(1) (C++20 이후)
template < class OutputIt, class ... Args >

OutputIt format_to ( OutputIt out,

std:: wformat_string < Args... > fmt, Args && ... args ) ;
(2) (C++20 이후)
template < class OutputIt, class ... Args >

OutputIt format_to ( OutputIt out, const std:: locale & loc,

std:: format_string < Args... > fmt, Args && ... args ) ;
(3) (C++20 이후)
template < class OutputIt, class ... Args >

OutputIt format_to ( OutputIt out, const std:: locale & loc,

std:: wformat_string < Args... > fmt, Args && ... args ) ;
(4) (C++20 이후)

형식 문자열 fmt 에 따라 args 를 포맷하고, 결과를 출력 반복자 out 에 기록합니다. 존재하는 경우, loc 은 로케일별 포맷팅에 사용됩니다.

다음과 동일합니다:

1) return std:: vformat_to ( std :: move ( out ) , fmt. str , std:: make_format_args ( args... ) ) ;
2) return std:: vformat_to ( std :: move ( out ) , fmt. str , std:: make_wformat_args ( args... ) ) ;
3) return std:: vformat_to ( std :: move ( out ) , loc, fmt. str , std:: make_format_args ( args... ) ) ;
4) return std:: vformat_to ( std :: move ( out ) , loc, fmt. str , std:: make_wformat_args ( args... ) ) ; .


CharT char 인 경우 (1,3) 번 오버로드에 해당하고, wchar_t 인 경우 (2,4) 번 오버로드에 해당한다.

이러한 오버로드는 OutputIt 가 다음 개념을 만족하는 경우에만 오버로드 해결에 참여합니다: std:: output_iterator < const CharT & > .

다음 조건 중 하나라도 충족되면, 동작은 정의되지 않습니다:

목차

매개변수

out - 출력 버퍼에 대한 반복자
fmt - 형식 문자열을 나타내는 객체. 형식 문자열은 다음으로 구성됩니다:
  • 일반 문자( { } 제외), 출력에 변경 없이 복사됨
  • 이스케이프 시퀀스 { { } } , 출력에서 각각 { } 로 대체됨
  • 치환 필드

각 치환 필드는 다음 형식을 가집니다:

{ arg-id (선택사항) } (1)
{ arg-id (선택사항) : format-spec } (2)
1) 형식 지정자 없는 치환 필드
2) 형식 지정자가 있는 치환 필드
arg-id - 포맷팅에 사용할 args 내 인자의 인덱스를 지정; 생략 시 인자는 순서대로 사용됨

형식 문자열 내 arg-id 는 모두 존재하거나 모두 생략되어야 함. 수동 및 자동 인덱싱을 혼합하는 것은 오류입니다.

format-spec - 해당 인자에 대한 std::formatter 특수화에 의해 정의된 형식 지정자. } 로 시작할 수 없음

(C++23부터)
(C++26부터)
  • 다른 포맷 가능 타입의 경우, 형식 지정자는 사용자 정의 formatter 특수화에 의해 결정됨
args... - 포맷팅할 인자들
loc - std::locale 로케일별 포맷팅에 사용되는 로케일

반환값

출력 범위의 끝을 지난 반복자.

예외

포매터나 반복자 연산에서 발생하는 모든 예외를 전파합니다.

참고 사항

형식 문자열이 상수 표현식이 아닌 경우 오류입니다 std::runtime_format 의 결과로 초기화된 경우를 제외하고 (C++26부터) . std::vformat_to 는 이 요구 사항이 없습니다.

예제

#include <format>
#include <iostream>
#include <iterator>
#include <string>
int main()
{
    std::string buffer;
    std::format_to
    (
        std::back_inserter(buffer), // < OutputIt
        "Hello, C++{}!\n",          // < fmt
        "20"                        // < arg
    );
    std::cout << buffer;
    buffer.clear();
    std::format_to
    (
        std::back_inserter(buffer), // < OutputIt
        "Hello, {0}::{1}!{2}",      // < fmt
        "std",                      // < arg {0}
        "format_to()",              // < arg {1}
        "\n",                       // < arg {2}
        "extra param(s)..."         // < unused
    );
    std::cout << buffer << std::flush;
    std::wstring wbuffer;
    std::format_to
    (
        std::back_inserter(wbuffer),// < OutputIt
        L"Hello, {2}::{1}!{0}",     // < fmt
        L"\n",                      // < arg {0}
        L"format_to()",             // < arg {1}
        L"std",                     // < arg {2}
        L"...is not..."             // < unused
        L"...an error!"             // < unused
    );
    std::wcout << wbuffer;
}

출력:

Hello, C++20!
Hello, std::format_to()!
Hello, std::format_to()!

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 3539 C++20 out could not be a move-only iterator it can be
P2216R3 C++20 throws std::format_error for invalid format string results in compile-time error instead
P2418R2 C++20 objects that are neither const-usable nor copyable
(such as generator-like objects) are not formattable
allow formatting these objects
P2508R1 C++20 there's no user-visible name for this facility the name basic_format_string is exposed

참고 항목

(C++20)
인수의 서식화된 표현을 새 문자열에 저장합니다
(함수 템플릿)
지정된 크기를 초과하지 않도록 출력 반복자를 통해 인수의 서식화된 표현을 출력합니다
(함수 템플릿)