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 オブジェクトを構築する
(公開メンバ関数)
operator=
bad_weak_ptr オブジェクトを置き換える
(公開メンバ関数)
what
説明文字列を返す
(公開メンバ関数)

std::bad_weak_ptr:: bad_weak_ptr

bad_weak_ptr ( ) noexcept ;
(1) (C++11以降)
bad_weak_ptr ( const bad_weak_ptr & other ) noexcept ;
(2) (C++11以降)

新しい bad_weak_ptr オブジェクトを構築します。実装定義のnull終端バイト文字列を持ち、 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 ;
(C++11以降)

説明文字列を返します。

戻り値

説明情報を含む実装定義のナル終端文字列へのポインタ。この文字列は std::wstring への変換と表示に適しています。ポインタは、少なくとも取得元の例外オブジェクトが破棄されるまで、または例外オブジェクトの非constメンバー関数(コピー代入演算子など)が呼び出されるまで有効であることが保証されます。

注記

実装は what() をオーバーライドすることが許可されていますが、必須ではありません。

std::exception から継承

メンバ関数

例外オブジェクトを破棄
( std::exception の仮想公開メンバ関数)
[virtual]
説明文字列を返す
( std::exception の仮想公開メンバ関数)

#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 によって管理されるオブジェクトへの弱参照
(クラステンプレート)