GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::expected::has_error - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. expected
  3. expected
  4. has_error

最終更新日時: 2026年09月07日 17時05分36秒 (JST)
Akira Takahashi が更新

履歴 編集

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

処理系

  • Clang: 24 ✅
  • GCC: 16.1 ❌
  • Visual C++: 2026 Update 6 ❌

関連項目

  • has_value
  • operator bool

参照

  • P3798R1 The unexpected in std::expected
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.