- memory[meta header]
- std[meta namespace]
- class[meta id-type]
- cpp11[meta cpp]
namespace std {
class bad_weak_ptr : public exception;
}- exception[link /reference/exception/exception.md]
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 |
what() |
エラー内容を取得する。文字列"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;
}
}- std::make_shared[link make_shared.md]
- std::weak_ptr[link weak_ptr.md]
- sp.reset[link shared_ptr/reset.md]
- std::exception[link /reference/exception/exception.md]
bad_weak_ptr
- C++11
- Clang: ??
- GCC: 4.4 [mark verified], 4.7.2(what()が"std::bad_weak_ptr"を返すので規格違反。バグ報告済み: #55847。4.7.3で修正されている。) [mark verified]
- ICC: ??
- Visual C++: 2008 (TR1) [mark verified], 2010 [mark verified], 2012 [mark verified], 2013 [mark verified]
- 2010までは
what()が"tr1::bad_weak_ptr"を返す。
- 2010までは