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 ❌