跳到主要内容

std::bad_weak_ptr

定义于头文件 <memory>

自 C++11 起
class bad_weak_ptr;

std::bad_weak_ptr 是 std::shared_ptr 的构造函数,当以 std::weak_ptr 作为参数,且该 std::weak_ptr 指向一个已被删除的对象时,所抛出的异常的类型。

继承

exception <- bad_weak_ptr

成员函数

pub(构造函数)构造一个新的 bad_weak_ptr 对象
(公共成员函数)
puboperator=替换 bad_weak_ptr 对象
(公共成员函数)
pubwhat返回解释性字符串
(公共成员函数)

std::bad_weak_ptr::bad_weak_ptr

自 C++11 起
bad_weak_ptr() noexcept;                              (1)
bad_weak_ptr( const bad_weak_ptr& other ) noexcept; (2)

构造一个新的 bad_weak_ptr 对象,其具有一个通过 what() 可访问的、由实现定义的、以空终止的字节字符串。

  1. 默认构造函数。
  2. 复制构造函数。如果 `*this` 和 other 都具有动态类型 std::bad_weak_ptr,则 `std::strcmp(what(), other.what()) == 0`。

参数

other - 要复制的另一个异常对象


std::bad_weak_ptr::operator=

自 C++11 起
bad_weak_ptr& operator=( const bad_weak_ptr& other ) noexcept;

将内容赋值给 other。如果 `*this` 和 other 都具有动态类型 std::bad_weak_ptr,则赋值后 `std::strcmp(what(), other.what()) == 0`。

参数

other - 要赋值的另一个异常对象

返回值

*this


std::bad_weak_ptr::what

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

返回解释性字符串。

参数

(无)

返回值

指向具有解释性信息的空终止字符串的指针。该字符串适用于转换为 std::wstring 并显示。保证该指针至少在当前异常对象被销毁之前,或在该异常对象上调用了非 const 成员函数(例如复制赋值运算符)之前都有效。

备注

实现允许但不要求重写 what()


继承自 std::exception

成员函数

pub(析构函数) [virtual] 销毁异常对象
(std::exception 的虚公共成员函数)
pubwhat [virtual] 返回解释性字符串
(std::exception 的虚公共成员函数)

示例

#include <memory>
#include <iostream>
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 2376C++11调用默认构造的 bad_weak_ptr 的 what() 被要求返回 "bad_weak_ptr"返回值是实现定义的

std::bad_weak_ptr

定义于头文件 <memory>

自 C++11 起
class bad_weak_ptr;

std::bad_weak_ptr 是 std::shared_ptr 的构造函数,当以 std::weak_ptr 作为参数,且该 std::weak_ptr 指向一个已被删除的对象时,所抛出的异常的类型。

继承

exception <- bad_weak_ptr

成员函数

pub(构造函数)构造一个新的 bad_weak_ptr 对象
(公共成员函数)
puboperator=替换 bad_weak_ptr 对象
(公共成员函数)
pubwhat返回解释性字符串
(公共成员函数)

std::bad_weak_ptr::bad_weak_ptr

自 C++11 起
bad_weak_ptr() noexcept;                              (1)
bad_weak_ptr( const bad_weak_ptr& other ) noexcept; (2)

构造一个新的 bad_weak_ptr 对象,其具有一个通过 what() 可访问的、由实现定义的、以空终止的字节字符串。

  1. 默认构造函数。
  2. 复制构造函数。如果 `*this` 和 other 都具有动态类型 std::bad_weak_ptr,则 `std::strcmp(what(), other.what()) == 0`。

参数

other - 要复制的另一个异常对象


std::bad_weak_ptr::operator=

自 C++11 起
bad_weak_ptr& operator=( const bad_weak_ptr& other ) noexcept;

将内容赋值给 other。如果 `*this` 和 other 都具有动态类型 std::bad_weak_ptr,则赋值后 `std::strcmp(what(), other.what()) == 0`。

参数

other - 要赋值的另一个异常对象

返回值

*this


std::bad_weak_ptr::what

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

返回解释性字符串。

参数

(无)

返回值

指向具有解释性信息的空终止字符串的指针。该字符串适用于转换为 std::wstring 并显示。保证该指针至少在当前异常对象被销毁之前,或在该异常对象上调用了非 const 成员函数(例如复制赋值运算符)之前都有效。

备注

实现允许但不要求重写 what()


继承自 std::exception

成员函数

pub(析构函数) [virtual] 销毁异常对象
(std::exception 的虚公共成员函数)
pubwhat [virtual] 返回解释性字符串
(std::exception 的虚公共成员函数)

示例

#include <memory>
#include <iostream>
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 2376C++11调用默认构造的 bad_weak_ptr 的 what() 被要求返回 "bad_weak_ptr"返回值是实现定义的