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

履歴 編集

function
<expected>

std::expected::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)

概要

エラー値を取得する。

事前条件

has_value() == false

戻り値

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

  • (1), (2) : エラー値を保持していたら、unex
  • (3), (4) : エラー値を保持していたら、std::move(unex)

例外

投げない

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

int main()
{
  std::expected<int, std::string> x = std::unexpected{"ERR"};
  assert(not x.has_value());
  std::cout << x.error() << std::endl;
}

出力

ERR

バージョン

言語

  • C++23

処理系

関連項目

参照