• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function template
    <expected>

    std::expected.void::operator==

    // expected<cv void, E>部分特殊化
    template<class T2, class E2> requires is_void_v<T2>
    friend constexpr bool operator==(const expected& x, const expected<T2, E2>& y); // (1)
    
    template<class E2>
    friend constexpr bool operator==(const expected& x, const unexpected<E2>& e);   // (2)
    // (2)により、下記オーバーロードが使用可能になる
    template<class E2>
    friend constexpr bool operator==(const unexpected<E2>& e, const expected& x);   // (3)
    

    概要

    • (1) : expectedオブジェクト同士の等値比較を行う。
    • (2), (3) : expectedオブジェクトとエラー値の等値比較を行う。

    テンプレートパラメータ制約

    • (1) : 式x.error() == y.error()適格であり、その結果をboolへ変換可能であること。
    • (2), (3) : 式x.error() == e.error()適格であり、その結果をboolへ変換可能であること。

    戻り値

    #include <cassert>
    #include <expected>
    
    int main()
    {
      std::expected<void, long>  x1;
      std::expected<void, short> y1;
      std::expected<void, long>  x2 = std::unexpected{1};
      std::expected<void, short> y2 = std::unexpected{1};
    
      // (1)
      assert(x1 == y1);
      assert(x2 == y2);
      assert(not (x1 == y2));
      assert(not (x2 == y1));
    
      // (2), (3)
      assert(x2 == std::unexpected{1});
      assert(std::unexpected{1} == x2);
    }
    

    出力

    バージョン

    言語

    • C++23

    処理系

    参照