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

Ale*_*exT 4 c++ exception c++11

请考虑以下代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

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

Ker*_* SB 6

rethrow_exception说的描述:

抛出:p引用的异常对象.

因此抛出的对象与指针所指的对象相同.指针可能会或可能不会引用原始的"当前异常",因为它current_exception可能会创建异常对象的副本,但这与您的问题无关.

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