• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    class
    <memory>

    std::bad_weak_ptr

    namespace std {
      class bad_weak_ptr : public exception;
    }
    

    概要

    bad_weak_ptrは、weak_ptrオブジェクトから破棄済みのshared_ptrオブジェクトを構築しようとした場合に発生する例外クラスである。

    破棄済みのshared_ptrを監視するweak_ptrオブジェクトからweak_ptr::lock()メンバ関数でshared_ptrオブジェクトを構築した場合、すでにshared_ptrが破棄されていれば、例外を送出することなく空のshared_ptrが返される。しかし、破棄済みのshared_ptrを監視するweak_ptrオブジェクトがshared_ptrのコンストラクタ引数として渡された場合には、この例外が送出される。

    メンバ関数

    名前 説明 対応バージョン
    bad_weak_ptr() noexcept;
    bad_weak_ptr(const bad_weak_ptr&) noexcept;
    コンストラクタ C++11
    virtual ~bad_weak_ptr() = default; デストラクタ C++11
    bad_weak_ptr& operator=(const bad_weak_ptr&) noexcept; 代入演算子 C++11
    virtual const char* what() const noexcept; エラー内容を取得する。文字列"bad_weak_ptr"が返される C++11

    #include <memory>
    #include <iostream>
    
    int main() {
      auto sp = std::make_shared<int>(42);
      std::weak_ptr<int> wp(sp);
    
      sp.reset();
      try {
        std::shared_ptr<int> i(wp);
      } catch(std::exception const& e) {
        std::cout << e.what() << std::endl;
      }
    }
    

    出力

    bad_weak_ptr
    

    バージョン

    言語

    • C++11

    処理系

    • Clang: ??
    • GCC: 4.4 , 4.7.2(what()が"std::bad_weak_ptr"を返すので規格違反。バグ報告済み: #55847。4.7.3で修正されている。)
    • ICC: ??
    • Visual C++: 2008 (TR1) , 2010 , 2012 , 2013
      • 2010まではwhat()"tr1::bad_weak_ptr"を返す。

    参照