gpt4 book ai didi

c++ - 与 std::current_exception 关联的数据的生命周期

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:19:12 26 4
gpt4 key购买 nike

考虑以下代码:

std::exception_ptr eptr{ std::current_exception() };
const char* msg = 0;
try {
if (eptr != std::exception_ptr{}) {
std::rethrow_exception(eptr);
}
} catch(const std::exception& ex) {
msg = ex.what();
}

我可以在 catch 之外使用 msg 吗?换句话说,ex 是否引用与 eptr 相同的异常实例?谢谢!

最佳答案

rethrow_exception 的描述说:

Throws: the exception object to which p refers.

因此抛出的对象与指针所指向的是同一个对象。指针可能会也可能不会引用原始的“当前异常”,因为 current_exception 可能会复制异常对象,但这与您的问题无关。

所以是的,您重新抛出的异常对象与 eptr 拥有的对象相同,因此只要该指针拥有它,它就会保持事件状态。

关于c++ - 与 std::current_exception 关联的数据的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21720579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com