types_bad_typeid

types_bad_typeid (bad_typeid 예외 클래스)

이 페이지는 C++ 표준 라이브러리의 std::bad_typeid 예외 클래스에 대해 설명해요. typeid 연산자를 다형성 타입의 역참조된 null 포인터 값에 적용할 때 이 예외가 발생해요. 이 클래스는 <typeinfo> 헤더에 정의되어 있어요.

출처: cppreference

본문

std::bad_typeidstd::exception에서 파생된 예외 클래스예요. typeid 연산자를 다형성 타입을 가리키는 null 포인터를 역참조한 결과에 적용하면 이 예외가 던져져요.

상속 다이어그램

std::bad_typeidstd::exception에서 직접 상속받아요.

멤버 함수

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

std::bad_typeid::bad_typeid

bad_typeid() throw(); (1) (until C++11)
bad_typeid() noexcept; (since C++11)
bad_typeid(const bad_typeid& other) throw(); (2) (until C++11)
bad_typeid(const bad_typeid& other) noexcept; (since C++11)

새로운 bad_typeid 객체를 생성해요. 이때 what()으로 접근할 수 있는 구현 정의된 null 종료 바이트 문자열이 함께 생성돼요.

매개변수

other - 복사할 다른 예외 객체

std::bad_typeid::operator=

bad_typeid& operator=(const bad_typeid& other) throw(); (until C++11)
bad_typeid& operator=(const bad_typeid& other) noexcept; (since C++11)

other의 내용을 대입해요. *thisother가 모두 동적 타입이 std::bad_typeid라면 대입 후 std::strcmp(what(), other.what()) == 0이 성립해요. (since C++11)

매개변수

other - 대입할 다른 예외 객체

반환값

*this

std::bad_typeid::what

virtual const char* what() const throw(); (until C++11)
virtual const char* what() const noexcept; (since C++11) (constexpr since C++26)

설명 문자열을 반환해요.

반환값

설명 정보를 담은 구현 정의된 null 종료 문자열에 대한 포인터예요. 이 문자열은 std::wstring으로 변환하거나 표시하기에 적합해요. 포인터는 얻은 예외 객체가 소멸될 때까지, 또는 예외 객체에 대해 비-const 멤버 함수(예: 복사 대입 연산자)가 호출될 때까지 유효함이 보장돼요.

상수 평가 중에는 반환된 문자열이 일반 리터럴 인코딩으로 인코딩돼요. (since C++26)

참고

구현은 what()을 재정의할 수 있지만 필수는 아니에요.

std::exception에서 상속받은 멤버

멤버 함수

멤버 함수 설명
(소멸자) [virtual] 예외 객체를 소멸해요 (std::exception의 가상 public 멤버 함수)
what [virtual] 설명 문자열을 반환해요 (std::exception의 가상 public 멤버 함수)

참고 사항

Feature-test macro 표준 기능
__cpp_lib_constexpr_exceptions 202411L (C++26) 예외 타입에 대한 constexpr

예제

#include <iostream>
#include <typeinfo>

struct S // The type has to be polymorphic
{
    virtual void f();
};

int main()
{
    S* p = nullptr;
    try
    {
        std::cout << typeid(*p).name() << '\n';
    }
    catch (const std::bad_typeid& e)
    {
        std::cout << e.what() << '\n';
    }
}

가능한 출력:

Attempted a typeid of NULL pointer!

더 알아보기 (Learn more)

cppreference