Namespaces
Variants

std:: bad_weak_ptr

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
헤더 파일에 정의됨 <memory>
class bad_weak_ptr ;
(C++11부터)

std::bad_weak_ptr std::shared_ptr 의 생성자들이 인자로 std::weak_ptr 을 받을 때, 해당 std::weak_ptr 이 이미 삭제된 객체를 참조하고 있는 경우 예외로 던져지는 객체의 타입입니다.

cpp/error/exception std-bad weak ptr-inheritance.svg

상속 다이어그램

목차

멤버 함수

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

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)

새로운 bad_weak_ptr 객체를 생성합니다. 이 객체는 구현에서 정의된 null-terminated byte string을 가지며, what() 를 통해 접근할 수 있습니다.

1) 기본 생성자입니다.
2) 복사 생성자입니다. 만약 * this other 모두 동적 타입이 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 의 내용으로 대입합니다. * this other 모두가 동적 타입 std::bad_weak_ptr 를 가질 경우, 대입 후 std:: strcmp ( what ( ) , other. what ( ) ) == 0 입니다.

매개변수

other - 대입할 다른 예외 객체

반환 값

* this

std::bad_weak_ptr:: what

virtual const char * what ( ) const noexcept ;
(since C++11)

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

반환 값

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

참고

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

std:: exception 에서 상속됨

멤버 함수

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

예제

#include <iostream>
#include <memory>
int main()
{
    std::shared_ptr<int> p1(new int(42));
    std::weak_ptr<int> wp(p1);
    p1.reset();
    try
    {
        std::shared_ptr<int> p2(wp);
    }
    catch (const std::bad_weak_ptr& e)
    {
        std::cout << e.what() << '\n';
    }
}

가능한 출력:

std::bad_weak_ptr

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2376 C++11 기본 생성된 bad_weak_ptr 에서 what 을 호출하면 "bad_weak_ptr" 을 반환해야 했음 반환값은 구현에 따라 정의됨

참고 항목

(C++11)
공유 객체 소유권 의미론을 가진 스마트 포인터
(클래스 템플릿)
(C++11)
std::shared_ptr 이 관리하는 객체에 대한 약한 참조
(클래스 템플릿)