最終更新日時:
が更新

履歴 編集

function
<expected>

std::expected::has_error(C++29)

constexpr bool has_error() const noexcept;

概要

エラー値を保持しているかを判定する。

効果

以下と等価である。

return !has_value();

例外

投げない

備考

  • has_value()の否定と同じであるが、エラー処理に注目したコードを書く際に、!x.has_value()よりも意図が読み取りやすくなる

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

int main()
{
  std::expected<std::string, int> x = "Hello";
  std::cout << x.has_error() << std::endl;

  std::expected<std::string, int> y = std::unexpected{42};
  std::cout << y.has_error() << std::endl;
}

出力

0
1

バージョン

言語

  • C++29

処理系

関連項目

参照