如何解决std :: exception_ptr复制构造函数可以抛出吗?
在我看来,标准允许std::exception_ptr
不使用引用计数(即std::exception_ptr
cctor可以复制它指向的异常对象)。这将意味着following code可能永远不会调用handle_eptr()
并且异常可以逃逸main()
并带来相关后果:
#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>
void handle_eptr(std::exception_ptr eptr) // passing by value is ok <---- ARE YOU SURE?
{
try {
if (eptr) {
std::rethrow_exception(eptr);
}
} catch(const std::exception& e) {
std::cout << "Caught exception \"" << e.what() << "\"\n";
}
}
int main()
{
std::exception_ptr eptr;
try {
std::string().at(1); // this generates an std::out_of_range
} catch(...) {
eptr = std::current_exception(); // capture
}
handle_eptr(eptr);
} // destructor for std::out_of_range called here,when the eptr is destructed
我正确吗?
解决方法
std :: exception_ptr复制构造函数可以抛出吗?
否。
标准说(最新草案):
[传播]
exception_ptr符合Cpp17NullablePointer的要求
[nullablepointer.requirements]
作为Cpp17NullablePointer要求的一部分的任何操作都不应通过异常退出。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。