Skip to content

Latest commit

 

History

History
70 lines (52 loc) · 1.83 KB

value_or.md

File metadata and controls

70 lines (52 loc) · 1.83 KB

value_or

  • expected[meta header]
  • function template[meta id-type]
  • std[meta namespace]
  • expected[meta class]
  • cpp23[meta cpp]
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;
}
  • value_or()[color ff0000]
  • std::unexpected[link ../unexpected.md]

出力

42
0

バージョン

言語

  • C++23

処理系

関連項目

参照