std::bad_weak_ptr
来自 cppreference.cn
定义于头文件 <memory> |
||
class bad_weak_ptr; |
(since C++11) | |
std::bad_weak_ptr
是由 std::shared_ptr 的构造函数抛出的异常对象的类型,这些构造函数接受 std::weak_ptr 作为参数,当 std::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) |
构造一个新的 bad_weak_ptr
对象,该对象带有一个实现定义的空终止字节字符串,可以通过 what() 访问。
1) 默认构造函数。
参数
other | - | 要复制的另一个异常对象 |
std::bad_weak_ptr::operator=
bad_weak_ptr& operator=( const bad_weak_ptr& other ) noexcept; |
(since 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 并显示。指针保证有效,至少在从中获取它的异常对象被销毁之前,或者直到调用异常对象上的非 const 成员函数(例如,复制赋值运算符)为止。
返回的字符串在使用常量求值期间的普通文字编码进行编码。 |
(since C++26) |
注释
允许但不要求实现重写 what()
。
继承自 std::exception
成员函数
[virtual] |
销毁异常对象 ( 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 管理的对象的弱引用 (类模板) |