在我看来,标准允许std::exception_ptr不使用引用计数(即std::exception_ptrcctor 可以制作它指向的异常对象的副本)。这意味着以下代码可能永远不会调用,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
Run Code Online (Sandbox Code Playgroud)
我对么?
std::exception_ptr 复制构造函数可以抛出吗?
不。
标准说(最新草案):
[传播]
exception_ptr 满足 Cpp17NullablePointer 的要求
[nullablepointer.requirements]
属于 Cpp17NullablePointer 要求的任何操作都不应通过异常退出。
| 归档时间: |
|
| 查看次数: |
96 次 |
| 最近记录: |