Namespaces
Variants

std:: print

From cppreference.net
< cpp ‎ | io
헤더 파일에 정의됨 <print>
template < class ... Args >
void print ( std:: format_string < Args... > fmt, Args && ... args ) ;
(1) (C++23부터)
template < class ... Args >

void print ( std:: FILE * stream,

std:: format_string < Args... > fmt, Args && ... args ) ;
(2) (C++23부터)

형식 문자열 fmt 에 따라 args 를 형식화하고, 결과를 출력 스트림에 출력합니다.

1) std :: print ( stdout , fmt, std:: forward < Args > ( args ) ... ) 와 동등합니다.
2) 만약 일반 리터럴 인코딩 이 UTF-8인 경우, 다음에 해당합니다: ( std :: enable_nonlocking_formatter_optimization < std:: remove_cvref_t < Args >> && ... )
? std:: vprint_unicode ( stream, fmt. str , std:: make_format_args ( args... ) )
: std :: vprint_unicode_buffered ( stream, fmt. str , std:: make_format_args ( args... ) ) ;
.
그렇지 않으면, 다음과 동일합니다: ( std :: enable_nonlocking_formatter_optimization < std:: remove_cvref_t < Args >> && ... )
? std:: vprint_nonunicode ( stream, fmt. str , std:: make_format_args ( args... ) )
: std :: vprint_nonunicode_buffered ( stream, fmt. str , std:: make_format_args ( args... ) ) ;
.

만약 std:: formatter < Ti, char > Args 내의 어떤 Ti 에 대해서도 BasicFormatter 요구사항을 충족하지 않는다면(이는 std::make_format_args 에 의해 요구됨), 동작은 정의되지 않습니다.

목차

매개변수

stream - 출력할 출력 파일 스트림
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... - 형식화할 인수들

예외

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_print 202207L (C++23) 형식화된 출력
202403L (C++26)
(DR23)
버퍼링되지 않은 형식화된 출력
202406L (C++26)
(DR23)
더 많은 형식화 가능 타입에 대한 버퍼링되지 않은 형식화된 출력 활성화
__cpp_lib_format 202207L (C++23) std::basic_format_string 노출

예제

#include <cstdio>
#include <filesystem>
#include <print>
int main()
{
    std::print("{2} {1}{0}!\n", 23, "C++", "Hello");  // overload (1)
    const auto tmp{std::filesystem::temp_directory_path() / "test.txt"};
    if (std::FILE* stream{std::fopen(tmp.c_str(), "w")})
    {
        std::print(stream, "File: {}", tmp.string()); // overload (2)
        std::fclose(stream);
    }
}

출력:

Hello C++23!

결함 보고서

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

DR 적용 대상 게시된 동작 올바른 동작
P3107R5 C++23 버퍼링된 출력 작업만 수행 가능 버퍼링되지 않은 출력 작업 수행 가능
P3235R3 C++23 P3107R5 에 의해 추가된 함수 이름들이
오해의 소지가 있었음
함수 이름 변경

참고 항목

(C++23)
std::print 과 동일하지만 각 출력이 추가적인 개행으로 종료됨
(함수 템플릿)
인수의 형식화된 표현을 출력함
(함수 템플릿)
(C++20)
인수의 형식화된 표현을 새 문자열에 저장함
(함수 템플릿)
(C++20)
출력 반복자를 통해 인수의 형식화된 표현을 기록함
(함수 템플릿)
형식화된 출력을 stdout , 파일 스트림 또는 버퍼에 출력함
(함수)