gpt4 book ai didi

c++ - std::exception_ptr 复制构造函数可以抛出吗?

转载 作者:行者123 更新时间:2023-12-01 14:36:32 26 4
gpt4 key购买 nike

在我看来,标准允许 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

我说的对吗?

最佳答案

Can std::exception_ptr copy constructor throw?

没有。

标准说(最新草案):

[propagation]

exception_­ptr meets the requirements of Cpp17NullablePointer

[nullablepointer.requirements]

No operation which is part of the Cpp17NullablePointer requirements shall exit via an exception.

关于c++ - std::exception_ptr 复制构造函数可以抛出吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63476703/

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