namespace std {
template <class E>
constexpr const E*
exception_ptr_cast(const exception_ptr& p) noexcept; // (1) C++26
template <class E>
void
exception_ptr_cast(const exception_ptr&&) = delete; // (2) C++26
}
概要
この関数は、std::exception_ptrオブジェクトに格納されている実際の例外型を取り出すために使用できる。std::rethrow_exception()関数でも同様のことはできるが、こちらの方が高速である可能性がある。
- (1):
std::exception_ptrオブジェクトを、指定された例外型Eへのポインタに変換して返す - (2): 右辺値の
std::exception_ptrオブジェクトを引数にするのは禁止されている
適格要件
戻り値
pがnullではなく、かつ型const E&のハンドラがその例外オブジェクトに対するマッチ条件を満たす場合、pが指す例外オブジェクトへのポインタを返す。そうでない場合は、nullptrを返す。
例外
投げない
例
#include <print>
#include <exception>
#include <stdexcept>
int main()
{
std::exception_ptr ep;
try {
throw std::runtime_error("error!");
}
catch (...) {
std::println("catch");
ep = std::current_exception(); // 処理中の例外ポインタを取得
}
if (const std::runtime_error* e = std::exception_ptr_cast<std::runtime_error>(ep)) {
std::println("exception: {}", e->what());
}
}
出力例
catch
exception: error!
バージョン
言語
- C++26
処理系
- Clang: 22 ✅
- GCC: 16 ✅
- Visual C++: 2022 Update 14 ❌