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

履歴 編集

function
<expected>

std::expected::value(C++23)

constexpr const T& value() const &;   // (1)
constexpr T& value() &;               // (2)
constexpr const T&& value() const &&; // (3)
constexpr T&& value() &&;             // (4)

概要

正常値を取得する。

戻り値

動作説明用のメンバ変数として、正常値を保持するvalを導入する。

  • (1), (2) : 正常値を保持していたら、val
  • (3), (4) : 正常値を保持していたら、std::move(val)

例外

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

int main()
{
  std::expected<int, std::string> x = 1;
  std::cout << x.value() << std::endl;

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

出力

1
throw:ERR

バージョン

言語

  • C++23

処理系

関連項目

参照