any_bad_any_cast
any_bad_any_cast (bad_any_cast 예외 클래스)
이 페이지에서는 C++ 표준 라이브러리의 std::bad_any_cast 예외 클래스에 대해 설명해요. std::any_cast가 값을 반환하는 형태로 실패할 때 이 예외 객체가 던져져요. 이 클래스는 <any> 헤더에 정의되어 있으며 C++17부터 사용할 수 있어요.
출처: cppreference
본문
정의
헤더 <any>에 정의됨 |
||
|---|---|---|
class bad_any_cast : public std::bad_cast; |
(C++17부터) |
값을 반환하는 형태의 std::any_cast가 실패할 때 던져질 객체의 타입을 정의해요.
멤버 함수
| (생성자) | 새로운 bad_any_cast 객체를 생성해요 (공개 멤버 함수) |
|---|---|
operator= |
bad_any_cast 객체를 치환해요 (공개 멤버 함수) |
what |
설명 문자열을 반환해요 (공개 멤버 함수) |
std::bad_any_cast::bad_any_cast
bad_any_cast() noexcept; |
(1) | (C++17부터) |
|---|---|---|
bad_any_cast(const bad_any_cast& other) noexcept; |
(2) | (C++17부터) |
구현에서 정의한 null 종료 바이트 문자열로 새 bad_any_cast 객체를 생성해요. 이 문자열은 what()을 통해 접근할 수 있어요.
매개변수
| other | - | 복사할 다른 예외 객체 |
std::bad_any_cast::operator=
| bad_any_cast& operator=(const bad_any_cast& other) noexcept; | | (C++17부터) |
other의 내용으로 *this의 내용을 할당해요. *this와 other가 모두 동적 타입이 std::bad_any_cast라면, 할당 후 std::strcmp(what(), other.what()) == 0이 돼요.
매개변수
| other | - | 할당에 사용할 다른 예외 객체 |
반환값
*this
std::bad_any_cast::what
| virtual const char* what() const noexcept; | | (C++17부터) |
설명 문자열을 반환해요.
반환값
구현에서 정의한 null 종료 문자열에 대한 포인터를 반환해요. 이 문자열은 std::wstring으로 변환 및 표시하기에 적합해요. 포인터는 적어도 해당 예외 객체가 소멸될 때까지, 또는 예외 객체에 대해 비-const 멤버 함수(예: 복사 할당 연산자)가 호출될 때까지 유효함이 보장돼요.
반환된 문자열은 상수 평가 중에 일반 리터럴 인코딩으로 인코딩돼요. (C++26부터)
참고
구현은 what()을 재정의할 수 있지만 필수는 아니에요.
std::bad_cast에서 상속받음
std::exception에서 상속받음
멤버 함수
| (소멸자) [virtual] | 예외 객체를 소멸해요 (std::exception의 가상 공개 멤버 함수) |
|---|---|
what [virtual] |
설명 문자열을 반환해요 (std::exception의 가상 공개 멤버 함수) |
예제
#include <any>
#include <cassert>
#include <print>
int main()
{
auto x = std::any(42);
assert(std::any_cast<int>(x) == 42); // OK
try
{
[[maybe_unused]] auto s = std::any_cast<std::string>(x); // throws
}
catch (const std::bad_any_cast& ex)
{
std::println("{}", ex.what());
}
}
가능한 출력:
bad any_cast