std::exception_ptr
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <exception> で定義
|
||
typedef /*unspecified*/ exception_ptr; |
(C++11以上) | |
std::exception_ptr は、投げられて std::current_exception によってキャプチャされた例外オブジェクトを管理するヌル可能なポインタライクな型です。 std::exception_ptr のインスタンスは別の関数や別のスレッドに渡し、投げ直して catch 節で処理することができます。
デフォルト構築された std::exception_ptr はヌルポインタであり、例外オブジェクトを指しません。
std::exception_ptr の2つのインスタンスは、それらがどちらもヌルであるか、どちらも同じ例外オブジェクトを指す場合にのみ、等しいと比較されます。
std::exception_ptr はいかなる算術型、列挙型、ポインタ型にも暗黙に変換することはできません。 bool には文脈的に変換することができ、ヌルであれば false に、そうでなければ true に評価されます。
std::exception_ptr によって参照される例外オブジェクトは、それを参照している std::exception_ptr が少なくともひとつ残っている限り、有効なままです。 std::exception_ptr は共有所有権を持つスマートポインタです (ノート: これは通常の例外オブジェクトの生存期間のルールに追加されます)。
std::exception_ptr は NullablePointer の要件を満たします。
例
Run this code
#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>
void handle_eptr(std::exception_ptr eptr) // passing by value is ok
{
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
出力:
Caught exception "basic_string::at"
関連項目
(C++11) |
例外オブジェクトから std::exception_ptr を作成します (関数テンプレート) |
(C++11) |
現在の例外を std::exception_ptr にキャプチャします (関数) |
(C++11) |
std::exception_ptr から例外を投げます (関数) |