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

履歴 編集

function
<expected>

std::bad_expected_access::error(C++23)

constexpr const E& error() const & noexcept;   // (1)
constexpr E& error() & noexcept;               // (2)
constexpr const E&& error() const && noexcept; // (3)
constexpr E&& error() && noexcept;             // (4)

概要

エラー値を取得する。

戻り値

動作説明用のメンバ変数として、エラー値を保持するunexを導入する。

例外

投げない

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

int main()
{
  std::expected<int, std::string> v = std::unexpected{"ERR"};
  try {
    std::cout << v.value() << std::endl;
  } catch (const std::bad_expected_access<std::string>& ex) {
    std::cout << "throw:" << ex.error() << std::endl;
  }
}

出力

throw:ERR

バージョン

言語

  • C++23

処理系

参照