bad_weak_ptr — std::bad_weak_ptr
bad_weak_ptr — std::bad_weak_ptr
std::bad_weak_ptr는 만료된(expired) std::weak_ptr를 기반으로 std::shared_ptr를 생성하려고 할 때 던져지는 예외 타입이에요.
weak_ptr가 이미 만료되어 가리키는 객체가 소멸된 상태라면, 그 weak_ptr로부터 shared_ptr를 만들 수 없어요. 이때 bad_weak_ptr 예외가 발생해요.
출처: cppreference
본문
std::bad_weak_ptr는 std::exception에서 파생된 예외 클래스예요.
class bad_weak_ptr : public std::exception;
만료된 weak_ptr를 shared_ptr 생성자에 전달하면, 생성자가 bad_weak_ptr 예외를 던져요. 일반적으로 weak_ptr::lock()을 사용하면 예외 대신 빈 shared_ptr를 얻을 수 있어서, 예외를 피하고 싶다면 lock()을 쓰는 게 편해요.
what() 멤버 함수를 통해 예외에 대한 설명 문자열을 얻을 수 있어요.
같이 보기
std::weak_ptrstd::shared_ptrstd::exception