format_format_error
format_format_error (형식 오류 예외 클래스)
std::format_error는 C++20에서 도입된 예외 타입으로, 형식화(formatting) 라이브러리에서 오류를 보고할 때 던져지는 객체의 타입을 정의해요. 이 예외는 형식 문자열이 잘못되었거나 인자와 형식 지정자가 맞지 않을 때 발생해요.
출처: cppreference
본문
std::format_error는 <format> 헤더에 정의되어 있어요.
| 헤더 | <format> |
|---|---|
| 정의 | class format_error; |
| 도입 | C++20 |
상속 다이어그램
std::format_error는 std::runtime_error를 상속하고, std::runtime_error는 std::exception을 상속해요.
멤버 함수
| 멤버 함수 | 설명 |
|---|---|
| (constructor) | 주어진 메시지로 새 format_error 객체를 생성해요 (공개 멤버 함수) |
| operator= | format_error 객체를 다른 객체로 교체해요 (공개 멤버 함수) |
std::format_error::format_error
| 생성자 | 설명 |
|---|---|
format_error(const std::string& what_arg) |
(1) |
format_error(const char* what_arg) |
(2) |
format_error(const format_error& other) noexcept |
(3) |
Parameters
| 매개변수 | 설명 |
|---|---|
what_arg |
설명 문자열 |
other |
복사할 다른 예외 객체 |
Exceptions
명시된 예외가 없어요.
Notes
std::format_error를 복사하는 것은 예외를 던지지 않도록 허용되기 때문에, 이 메시지는 일반적으로 별도로 할당된 참조 카운트 문자열로 내부에 저장돼요. 이것이 std::string&&을 받는 생성자가 없는 이유이기도 해요. 어차피 내용을 복사해야 하기 때문이에요.
파생된 표준 예외 클래스는 공개적으로 접근 가능한 복사 생성자를 가져야 해요. what()으로 얻는 설명 문자열이 원본 객체와 복사된 객체에서 동일하다면, 복사 생성자는 암시적으로 정의될 수 있어요.
std::format_error::operator=
| 연산자 | 설명 |
|---|---|
format_error& operator=(const format_error& other) noexcept; |
other의 내용을 *this에 대입해요. *this와 other가 모두 동적 타입이 std::format_error라면, 대입 후 std::strcmp(what(), other.what()) == 0이 성립해요. 복사 대입 연산자에서는 예외가 던져지지 않아요.
Parameters
| 매개변수 | 설명 |
|---|---|
other |
대입할 다른 예외 객체 |
Return value
*this
Notes
파생된 표준 예외 클래스는 공개적으로 접근 가능한 복사 대입 연산자를 가져야 해요. what()으로 얻는 설명 문자열이 원본 객체와 복사된 객체에서 동일하다면, 복사 대입 연산자는 암시적으로 정의될 수 있어요.
std::runtime_error로부터 상속
std::format_error는 std::runtime_error로부터 상속받아요.
std::exception으로부터 상속
멤버 함수
| 멤버 함수 | 설명 |
|---|---|
| (소멸자) [virtual] | 예외 객체를 소멸해요 (std::exception의 가상 공개 멤버 함수) |
| what [virtual] | 설명 문자열을 반환해요 (std::exception의 가상 공개 멤버 함수) |
Example
#include <format>
#include <print>
#include <string_view>
#include <utility>
int main()
{
try
{
auto x13{37};
auto args{std::make_format_args(x13)};
std::ignore = std::vformat("{:()}", args); // throws
}
catch(const std::format_error& ex)
{
std::println("{}", ex.what());
}
}
Possible output:
format error: failed to parse format-spec