std:: to_chars_result
|
헤더에 정의됨
<charconv>
|
||
|
struct
to_chars_result
;
|
(C++17부터) | |
std::to_chars_result
는
std::to_chars
의 반환 타입입니다. 이 타입은 기본 클래스를 가지지 않으며, 오직 다음 멤버들만을 가지고 있습니다.
목차 |
데이터 멤버
| 멤버 이름 | 정의 |
|
ptr
|
char
*
타입의 포인터
(public member object) |
|
ec
|
std::errc
타입의 에러 코드
(public member object) |
멤버 및 프렌드 함수
operator== (std::to_chars_result)
|
friend
bool
operator
==
(
const
to_chars_result
&
,
const to_chars_result & ) = default ; |
(C++20 이후) | |
두 인자를
기본 비교
를 사용하여 비교합니다
(이는 각각
operator
==
를 사용하여
ptr
과
ec
를 비교합니다).
이 함수는 일반적인 비한정 또는 한정 조회 에는 보이지 않으며, std::to_chars_result가 인자의 연관 클래스일 때만 인자 종속 조회 에 의해 찾을 수 있습니다.
!=
연산자는
operator==
로부터
합성됩니다
.
operator bool
|
constexpr
explicit
operator
bool
(
)
const
noexcept
;
|
(C++26부터) | |
변환이 성공적인지 확인합니다. ec == std:: errc { } 를 반환합니다.
참고 사항
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_to_chars
|
201611L
|
(C++17) | 기본 문자열 변환 ( std::to_chars , std::from_chars ) |
202306L
|
(C++26) | <charconv> 함수의 성공 또는 실패 테스트 |
예제
#include <array> #include <charconv> #include <iostream> #include <string_view> #include <system_error> void show_to_chars(auto... format_args) { std::array<char, 10> str; #if __cpp_lib_to_chars >= 202306L and __cpp_structured_bindings >= 202406L // C++26 구조화된 바인딩 선언을 조건으로 사용 (P0963) // 및 C++26 to_chars_result::operator bool()을 오류 검사에 사용 (P2497) if (auto [ptr, ec] = std::to_chars(str.data(), str.data() + str.size(), format_args...)) std::cout << std::string_view(str.data(), ptr) << '\n'; else std::cout << std::make_error_code(ec).message() << '\n'; #elif __cpp_lib_to_chars >= 202306L // C++26 to_chars_result::operator bool()을 오류 검사에 사용 (P2497) if (auto result = std::to_chars(str.data(), str.data() + str.size(), format_args...)) std::cout << std::string_view(str.data(), result.ptr) << '\n'; else std::cout << std::make_error_code(result.ec).message() << '\n'; #else // C++17 if-with-initializer 및 구조화된 바인딩으로 폴백 if (auto [ptr, ec] = std::to_chars(str.data(), str.data() + str.size(), format_args...); ec == std::errc()) std::cout << std::string_view(str.data(), ptr - str.data()) << '\n'; else std::cout << std::make_error_code(ec).message() << '\n'; #endif } int main() { show_to_chars(42); show_to_chars(+3.14159F); show_to_chars(-3.14159, std::chars_format::fixed); show_to_chars(-3.14159, std::chars_format::scientific, 3); show_to_chars(3.1415926535, std::chars_format::fixed, 10); }
가능한 출력:
42 3.14159 -3.14159 -3.142e+00 Value too large for defined data type
참고 항목
|
(C++17)
|
정수 또는 부동소수점 값을 문자 시퀀스로 변환합니다
(함수) |