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

履歴 編集

function template
<expected>

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

template<class U> constexpr T value_or(U&& v) const &; // (1)
template<class U> constexpr T value_or(U&& v) &&;      // (2)

概要

正常値もしくは指定された値を取得する。

適格要件

戻り値

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

int main()
{
  std::expected<int, std::string> x = 42;
  std::cout << x.value_or(0) << std::endl;

  std::expected<int, std::string> y = std::unexpected{"ERR"};
  std::cout << y.value_or(0) << std::endl;
}

出力

42
0

バージョン

言語

  • C++23

処理系

関連項目

参照