最終更新日時(UTC):
が更新

履歴 編集

class template
<expected>

std::bad_expected_access(C++23)

namespace std {
  template<class E>
  class bad_expected_access : public bad_expected_access<void> { ... };

  template<>
  class bad_expected_access<void> : public exception { ... };
}

概要

bad_expected_accessは、expectedオブジェクトがエラー値を保持しているとき正常値にアクセスした場合に発生する例外である。

メンバ関数

名前 説明 対応バージョン
(constructor) コンストラクタ C++23
(destructor) デストラクタ C++23
operator= コピー/ムーブ代入演算子 C++23
error エラー値を取得する C++23
what エラー理由の文字列を取得する C++23

#include <cassert>
#include <expected>
#include <iostream>
#include <string>

int main()
{
  std::expected<int, std::string> x = std::unexpected{"invalid"};

  try {
    assert(not x.has_value());
    int value = x.value();  // bad_expected_access例外発生
    std::cout << "value: " << value << std::endl;
  }
  catch (const std::bad_expected_access<std::string>& ex) {
    std::cout << "error: " << ex.error() << std::endl;
  }
}

出力

error: invalid

バージョン

言語

  • C++23

処理系

関連項目

参照