memory_bad_weak_ptr

memory_bad_weak_ptr (std::bad_weak_ptr)

<memory> 헤더에 정의되어 있어요. std::weak_ptr를 인자로 받는 std::shared_ptr 생성자가, 그 std::weak_ptr가 이미 삭제된 객체를 가리킬 때 예외로 던지는 객체의 타입이에요. C++11부터 도입됐어요.

출처: cppreference

본문

class bad_weak_ptr;

(C++11부터)

std::bad_weak_ptrstd::weak_ptr를 인자로 받는 std::shared_ptr 생성자들이 예외로 던지는 타입이에요. 해당 std::weak_ptr가 이미 삭제된 객체를 가리킬 때 던져져요.

상속 관계: std::bad_weak_ptr는 예외 계통의 클래스에서 파생돼요.

멤버 함수

  • (생성자): 새 bad_weak_ptr 객체 생성 (공개 멤버 함수)
  • operator=: bad_weak_ptr 객체 대체 (공개 멤버 함수)
  • what: 설명 문자열 반환 (공개 멤버 함수)

std::bad_weak_ptr::bad_weak_ptr

bad_weak_ptr() noexcept;                        // (1) since C++11
bad_weak_ptr( const bad_weak_ptr& other ) noexcept;  // (2) since C++11

what()로 접근할 수 있는 구현 정의 널 종료 바이트 문자열을 가진 새 bad_weak_ptr 객체를 구성해요.

    1. 기본 생성자.
    1. 복사 생성자. *thisother 모두 동적 타입이 std::bad_weak_ptr이면 std::strcmp(what(), other.what()) == 0이에요.

매개변수

  • other: 복사할 다른 예외 객체

std::bad_weak_ptr::operator=

bad_weak_ptr& operator=( const bad_weak_ptr& other ) noexcept;

(C++11부터)

other의 내용으로 대입해요. *thisother 모두 동적 타입이 std::bad_weak_ptr이면 대입 후 std::strcmp(what(), other.what()) == 0이에요.

매개변수

  • other: 대입할 다른 예외 객체
std::weak_ptr<int> wp = /* 만료된 weak_ptr */;
try {
    std::shared_ptr<int> sp(wp);   // 이미 삭제된 객체 → bad_weak_ptr 예외
} catch (const std::bad_weak_ptr& e) {
    // 처리
}

더 알아보기 (Learn more)

cppreference