std:: make_format_args, std:: make_wformat_args
|
헤더 파일에 정의됨
<format>
|
||
|
template
<
class
Context
=
std::
format_context
,
class
...
Args
>
/*format-arg-store*/
<
Context, Args...
>
|
(1) | (C++20 이후) |
|
template
<
class
...
Args
>
/*format-arg-store*/
<
std::
wformat_context
, Args...
>
|
(2) | (C++20 이후) |
서식 인수들의 배열을 저장하며 std::basic_format_args<Context> 로 암시적으로 변환될 수 있는 객체를 반환합니다.
다음의 경우 동작은 정의되지 않습니다:
typename
Context
::
template
formatter_type
<
std::
remove_const_t
<
Ti
>>
가
BasicFormatter
요구사항을 충족하지 않는 경우
Args
내의 임의의
Ti
에 대해.
프로그램은
Args
내의 어떤 타입
Ti
에 대해서도
Ti
가
__formattable_with
<
Context
>
를 만족하지 않으면 형식이 잘못되었습니다.
목차 |
매개변수
| args... | - | 서식 지정 인수로 사용될 값들 |
반환값
서식 지정 인수를 보유하는 객체입니다.
각 인수
t
(타입
T
)에 대해,
TD
를
std::
remove_const_t
<
std::
remove_reference_t
<
T
>>
로 정의합니다. 결과에 포함되는 해당
std::basic_format_arg
는 아래와 같이 결정됩니다:
-
만약
TD가 bool 이거나Context::char_type인 경우, std::basic_format_arg 은 t 를 저장합니다; -
그렇지 않고,
TD가 char 이고Context::char_type이 wchar_t 인 경우, std::basic_format_arg 는 static_cast < wchar_t > ( static_cast < unsigned char > ( t ) ) 를 저장합니다; -
그렇지 않고,
TD가 크기가 int 보다 크지 않은 부호 있는 정수 형식인 경우, std::basic_format_arg 는 static_cast < int > ( t ) 를 저장합니다; -
그렇지 않고,
TD가 크기가 unsigned int 보다 크지 않은 unsigned integer type인 경우, std::basic_format_arg 는 static_cast < unsigned int > ( t ) 를 저장한다; -
그렇지 않고,
TD가 크기가 long long 보다 크지 않은 부호 있는 정수 타입인 경우, std::basic_format_arg 는 static_cast < long long > ( t ) 를 저장한다; -
그렇지 않고,
TD가 크기가 unsigned long long 보다 크지 않은 부호 없는 정수 타입인 경우, std::basic_format_arg 는 static_cast < unsigned long long > ( t ) 를 저장한다; -
그렇지 않고,
TD가 float , double , 또는 long double 인 경우, std::basic_format_arg 는 t 를 저장한다; -
그렇지 않고,
TD가 std::basic_string_view 또는 std::basic_string 특수화이며TD::char_type이Context::char_type와 동일한 경우, std::basic_format_arg 는 std:: basic_string_view < Context :: char_type > ( t. data ( ) , t. size ( ) ) 를 저장합니다; - 그렇지 않고, 만약 std:: decay_t < TD > 가 Context :: char_type * 이거나 const Context :: char_type * 라면, std::basic_format_arg 은 static_cast < const Context :: char_type * > ( t ) 를 저장한다;
- 그렇지 않고, std:: is_void_v < std:: remove_pointer_t < TD >> 가 true 이거나 std:: is_null_pointer_v < TD > 가 true 인 경우, std::basic_format_arg 는 static_cast < const void * > ( t ) 를 저장한다;
-
그렇지 않으면,
std::basic_format_arg
는
std::
basic_format_arg
<
Context
>
::
handle
을
t에 저장하며,handle::format()에 필요한 추가 데이터도 함께 저장합니다.
참고 사항
사용자 정의 타입에 대한 포맷팅 인수는 참조 의미론을 가지며 args 의 수명을 연장하지 않습니다. args 가 반환값보다 더 오래 유지되도록 하는 것은 프로그래머의 책임입니다. 일반적으로 결과는 포맷팅 함수의 인수로만 사용됩니다.
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_format_uchar
|
202311L
|
(C++20)
(DR) |
코드 유닛의 부호 없는 정수 형식 지정 |
예제
#include <array> #include <format> #include <iostream> #include <string_view> void raw_write_to_log(std::string_view users_fmt, std::format_args&& args) { static int n{}; std::clog << std::format("{:04} : ", n++) << std::vformat(users_fmt, args) << '\n'; } template<typename... Args> constexpr void log(Args&&... args) { // 포맷팅 문자열 "{} "... 생성 std::array<char, sizeof...(Args) * 3 + 1> braces{}; constexpr const char c[4] = "{} "; for (auto i{0uz}; i != braces.size() - 1; ++i) braces[i] = c[i % 3]; braces.back() = '\0'; raw_write_to_log(std::string_view{braces.data()}, std::make_format_args(args...)); } template<typename T> const T& unmove(T&& x) { return x; } int main() { log("Number", "of", "arguments", "is", "arbitrary."); log("Any type that meets the BasicFormatter requirements", "can be printed."); log("For example:", 1, 2.0, '3', "*42*"); raw_write_to_log("{:02} │ {} │ {} │ {}", std::make_format_args(unmove(1), unmove(2.0), unmove('3'), "4")); }
출력:
0000 : Number of arguments is arbitrary. 0001 : Any type that meets the BasicFormatter requirements can be printed. 0002 : For example: 1 2.0 3 *42* 0003 : 01 │ 2.0 │ 3 │ 4
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| P2418R2 | C++20 |
const-usable도 복사 가능하지도 않은 객체들
(제너레이터형 객체들)은 포맷팅 불가 |
이러한 객체들의 포맷팅 허용 |
| P2905R2 | C++20 |
make_format_args
가 전달 참조로 rvalue 인수들을 수락함
|
lvalue 참조만 취함 |
| P2909R4 | C++20 |
char
또는
wchar_t
가 범위를 벗어난 부호 없는 정수 값으로
포맷팅될 수 있음 |
코드 유닛들은 그러한 포맷팅 전에 해당
부호 없는 타입으로 변환됨 |
| LWG 3631 | C++20 | P2418R2 이후 cv-qualified 인수들이 잘못 처리됨 | 처리 방식 수정됨 |
참고 항목
|
(C++20)
(C++20)
(C++20)
|
모든 포매팅 인수에 대한 접근을 제공하는 클래스
(클래스 템플릿) |
|
(C++20)
|
타입 삭제된 인수 표현을 사용하는
std::format
의 비템플릿 변형
(함수) |
|
(C++20)
|
타입 삭제된 인수 표현을 사용하는
std::format_to
의 비템플릿 변형
(함수 템플릿) |