Namespaces
Variants

std::bad_any_cast

cppreference.net에서
 
 
유틸리티 라이브러리
일반 유틸리티
관계 연산자 (C++20에서 폐기됨)
정수 비교 함수
(C++20)(C++20)(C++20)    
(C++20)
스왑 및 타입 연산
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
일반 어휘 타입
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
헤더 <any>
class bad_any_cast : public std::bad_cast;
에 정의됨 (C++17부터)

실패 시 std::any_cast의 값을 반환하는 형태에 의해 던져지는 객체의 타입을 정의합니다.

멤버 함수

(생성자)
새로운 bad_any_cast 객체를 생성함
(public 멤버 함수)
operator=
bad_any_cast 객체를 대체함
(public 멤버 함수)
what
설명 문자열을 반환함
(public 멤버 함수)

std::bad_any_cast:: bad_any_cast

bad_any_cast ( ) noexcept ;
(1) (since C++17)
bad_any_cast ( const bad_any_cast & other ) noexcept ;
(2) (since C++17)

새로운 bad_any_cast 객체를 생성합니다. 구현 정의된 null-terminated 바이트 문자열을 포함하며, 이는 what() 을 통해 접근 가능합니다.

1) 기본 생성자.
2) 복사 생성자. * this 와 other 모두 동적 타입 std::bad_any_cast 를 가질 경우, std:: strcmp ( what ( ) , other. what ( ) ) == 0 입니다.

매개변수

other - 복사할 다른 예외 객체

std::bad_any_cast:: operator=

bad_any_cast & operator = ( const bad_any_cast & other ) noexcept ;
(C++17부터)

내용을 other 의 내용으로 대입합니다. * 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부터)

설명 문자열을 반환합니다.

반환값

설명 정보를 담은 구현 정의 널 종료 문자열에 대한 포인터. 이 문자열은 std::wstring 으로 변환 및 표시하기에 적합합니다. 이 포인터는 최소한 해당 포인터를 얻은 예외 객체가 소멸되거나, 예외 객체의 비상수 멤버 함수(예: 복사 할당 연산자)가 호출되기 전까지는 유효함이 보장됩니다.

참고

구현체는 what() 을 재정의할 수 있지만 필수는 아닙니다.

std:: bad_cast 에서 상속됨

std::exception에서 상속됨

멤버 함수

[virtual]
예외 객체를 파괴함
( std::exception 의 virtual public 멤버 함수)
[virtual]
설명 문자열을 반환함
( std::exception 의 virtual public 멤버 함수)

예제

#include <any>
#include <cassert>
#include <print>
int main()
{
    auto x = std::any(42);
    assert(std::any_cast<int>(x) == 42); // 정상
    try
    {
        [[maybe_unused]] auto s = std::any_cast<std::string>(x); // 예외 발생
    }
    catch (const std::bad_any_cast& ex)
    {
        std::println("{}", ex.what());
    }
}

가능한 출력:

bad any_cast