std::exception_ptr
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <exception> 中定义
|
||
typedef /*unspecified*/ exception_ptr; |
(C++11 起) | |
std::exception_ptr
是一个可为空指针类型,管理一个被抛出的异常对象和拍摄的std::current_exception。 std::exception_ptr
的一个实例传递给另一个函数,可能在另一个线程,可能会被重新抛出异常和处理的catch子句.原文:
std::exception_ptr
is a nullable pointer-like type that manages an exception object which has been thrown and captured with std::current_exception. An instance of std::exception_ptr
may be passed to another function, possibly on another thread, where the exception may be rethrown and handled with a catch clause.默认构造
std::exception_ptr
是一个空指针,它不指向一个异常对象,.原文:
Default-constructed
std::exception_ptr
is a null pointer, it does not point to an exception object.比较平等的,只有当他们都是null,或都指向相同的异常对象的两个实例
std::exception_ptr
.原文:
Two instances of
std::exception_ptr
compare equal only if they are both null or both point at the same exception object.std::exception_ptr
是任何算术,枚举,或指针类型不能隐式转换。它转换为bool
原文:
std::exception_ptr
is not implicitly convertible to any arithmetic, enumeration, or pointer type. It is convertible to bool
.异常对象所引用的
std::exception_ptr
仍然有效,只要仍然有至少一个std::exception_ptr
引用它:std::exception_ptr
是一个共享所有权的智能指针.原文:
The exception object referenced by an
std::exception_ptr
remains valid as long as there remains at least one std::exception_ptr
that is referencing it: std::exception_ptr
is a shared-ownership smart pointer.[编辑] 示例
#include <iostream> #include <string> #include <exception> #include <stdexcept> void handle_eptr(std::exception_ptr eptr) // passing by value is ok { try { if (eptr != std::exception_ptr()) { 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
输出:
Caught exception "basic_string::at"
[编辑] 另请参阅
(C++11) |
创建一个异常对象的std::exception_ptr 原文: creates an std::exception_ptr from an exception object (函数模板) |
(C++11) |
captures the current exception in a std::exception_ptr (函数) |
(C++11) |
从std::exception_ptr抛出的异常 原文: throws the exception from an std::exception_ptr (函数) |