名前空間
変種
操作

operator==(std::expected)

From cppreference.com
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
 
プライマリテンプレート
template< class T2, class E2 >

    requires (!std::is_void_v<T2>)
friend constexpr bool operator==( const expected& lhs,

                                  const std::expected<T2, E2>& rhs );
(1) (C++23から)
template< class E2 >

friend constexpr bool operator==( const expected& lhs,

                                  const std::unexpected<E2>& unex );
(2) (C++23から)
template< class T2 >
friend constexpr bool operator==( const expected& lhs, const T2& val );
(3) (C++23から)
void 部分特殊化
template< class T2, class E2 >

  requires std::is_void_v<T2>
friend constexpr bool operator==( const expected& lhs,

                                  const std::expected<T2, E2>& rhs );
(4) (C++23から)
template< class E2 >

friend constexpr bool operator==( const expected& lhs,

                                  const std::unexpected<E2>& unex );
(5) (C++23から)

std::expected オブジェクトに対して比較演算を実行する。

1) 2つの std::expected オブジェクトを比較する。両方の lhsrhs が等しい期待値を含んでいる場合、または両方が等しい予期せぬ値を含んでいる場合にのみ、オブジェクトは等しいと比較される。

以下のいずれかの式が不正であるか、その結果が bool に変換できない場合、プログラムは不正である。

(C++26まで)

このオーバーロードは、以下のすべての式が整形式であり、その結果が bool に変換できる場合にのみ、オーバーロード解決に参加する。

(C++26以降)
  • *lhs == *rhs
  • lhs.error() == rhs.error()
2) std::expected オブジェクトを std::unexpected オブジェクトと比較する。lhsunex.error() と等しい予期せぬ値を含んでいる場合にのみ、オブジェクトは等しいと比較される。

lhs.error() == unex.error() が不正であるか、その結果が bool に変換できない場合、プログラムは不正である。

(C++26まで)

このオーバーロードは、式 lhs.error() == unex.error() が整形式であり、その結果が bool に変換できる場合にのみ、オーバーロード解決に参加する。

(C++26以降)
3) std::expected オブジェクトを期待値と比較する。lhsval と等しい期待値を含んでいる場合にのみ、オブジェクトは等しいと比較される。

*lhs == val が不正であるか、その結果が bool に変換できない場合、プログラムは不正である。

(C++26まで)

このオーバーロードは、以下のすべての条件が満たされた場合にのみオーバーロード解決に参加する。

  • T2std::expected の特殊化ではない。
  • *lhs == val は整形式であり、その結果は bool に変換できる。
(C++26以降)
4) 2つの std::expected オブジェクトを比較する。両方の lhsrhs が期待値を表す場合、または両方が等しい予期せぬ値を含んでいる場合にのみ、オブジェクトは等しいと比較される。

lhs.error() == rhs.error() が不正であるか、その結果が bool に変換できない場合、プログラムは不正である。

(C++26まで)

このオーバーロードは、式 lhs.error() == rhs.error() が整形式であり、その結果が bool に変換できる場合にのみ、オーバーロード解決に参加する。

(C++26以降)
5) std::expected オブジェクトを std::unexpected オブジェクトと比較する。lhsunex.error() と等しい予期せぬ値を含んでいる場合にのみ、オブジェクトは等しいと比較される。

lhs.error() == unex.error() が不正であるか、その結果が bool に変換できない場合、プログラムは不正である。

(C++26まで)

このオーバーロードは、式 lhs.error() == unex.error() が整形式であり、その結果が bool に変換できる場合にのみ、オーバーロード解決に参加する。

(C++26以降)

これらの関数は、通常の非修飾または修飾ルックアップでは可視ではなく、std::expected<T, E> が引数の関連クラスである場合にのみ、引数依存ルックアップによって見つけることができる。

!= 演算子は operator== から合成される。

目次

[編集] パラメータ

lhs, rhs - 比較する std::expected オブジェクト
unex - lhs と比較する std::unexpected
val - lhs に含まれる期待値と比較する値

[編集] 戻り値

1) lhs.has_value() != rhs.has_value() ? false :
    (lhs.has_value() ? *lhs == *rhs : lhs.error() == rhs.error())
2) !lhs.has_value() && static_cast<bool>(lhs.error() == unex.error())
3) lhs.has_value() && static_cast<bool>(*lhs == val)
4) lhs.has_value() != rhs.has_value() ? false :
    lhs.has_value() || static_cast<bool>(lhs.error() == rhs.error())
5) !lhs.has_value() && static_cast<bool>(lhs.error() == unex.error())

[編集] 例外

比較がスローする場合に、その例外をスローする。

[編集] 備考

機能テストマクロ 規格 機能
__cpp_lib_constrained_equality 202411L (C++26) std::expected の制約付き比較演算子

[編集]

#include <expected>
#include <iostream>
#include <string_view>
 
using namespace std::string_view_literals;
 
int main()
{
    auto x1{"\N{GREEN HEART}"sv};
    auto x2{"\N{CROSS MARK}"sv};
    std::expected<std::string_view, int> e1{x1}, e2{x1}, e3{x2};
    std::unexpected u1{13};
 
    std::cout << "Overload (1):\n"
              << e1.value() << (e1 == e2 ? " == " : " != ") << *e2 << '\n'
              << e1.value() << (e1 != e3 ? " != " : " == ") << *e3 << "\n\n";
 
    std::cout << "Overload (2):\n"
              << e1.value() << (e1 == u1 ? " == " : " != ") << u1.error() << '\n';
    e1 = std::unexpected{13};
    std::cout << e1.error() << (e1 == u1 ? " == " : " != ") << u1.error() << '\n';
    e1 = std::unexpected{31};
    std::cout << e1.error() << (e1 != u1 ? " != " : " == ") << u1.error() << '\n';
 
    std::cout << "Overload (3):\n"
              << *e1 << (e1 == x1 ? " == " : " != ") << x1 << '\n'
              << *e1 << (e1 != x2 ? " != " : " == ") << x2 << "\n\n";
}

出力

Overload (1):
💚 == 💚
💚 != ❌
 
Overload (2):
💚 != 13
13 == 13
31 != 13
 
Overload (3):
💚 == 💚
💚 != ❌

[編集] 関連項目

期待されない値として表現される
(class template) [編集]
English 日本語 中文(简体) 中文(繁體)