Skip to content

Latest commit

 

History

History
81 lines (63 loc) · 2.22 KB

bad_expected_access.md

File metadata and controls

81 lines (63 loc) · 2.22 KB

bad_expected_access

  • expected[meta header]
  • class template[meta id-type]
  • std[meta namespace]
  • cpp23[meta cpp]
namespace std {
  template<class E>
  class bad_expected_access : public bad_expected_access<void> { ... };

  template<>
  class bad_expected_access<void> : public exception { ... };
}
  • exception[link /reference/exception/exception.md]

概要

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;
  }
}
  • std::bad_expected_access[color ff0000]
  • std::unexpected[link unexpected.md]

出力

error: invalid

バージョン

言語

  • C++23

処理系

関連項目

参照