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

履歴 編集

function
<expected>

std::expected::operator*(C++23)

constexpr const T& operator*() const & noexcept;   // (1)
constexpr T& operator*() & noexcept;               // (2)
constexpr T&& operator*() && noexcept;             // (3)
constexpr const T&& operator*() const && noexcept; // (4)

概要

正常値を取得する。

事前条件

has_value() == true

戻り値

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

例外

投げない

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

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

  std::expected<int, std::string> y = std::unexpected{"ERR"};
//std::cout << *y << std::endl;
  // エラー値を保持する y に対する operator* 呼び出しは未定義動作
}

出力

1

バージョン

言語

  • C++23

処理系

関連項目

参照