Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      operator==(std::expected)

      From cppreference.com
      <cpp‎ |utility‎ |expected
       
       
      Utilities library
       
       
      Primary template
      template<class T2,class E2>

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

                                       conststd::expected<T2, E2>& rhs);
      (1)(since C++23)
      template<class E2>

      friendconstexprbool operator==(const expected& lhs,

                                       conststd::unexpected<E2>& unex);
      (2)(since C++23)
      template<class T2>
      friendconstexprbool operator==(const expected& lhs,const T2& val);
      (3)(since C++23)
      void partial specialization
      template<class T2,class E2>

        requiresstd::is_void_v<T2>
      friendconstexprbool operator==(const expected& lhs,

                                       conststd::expected<T2, E2>& rhs);
      (4)(since C++23)
      template<class E2>

      friendconstexprbool operator==(const expected& lhs,

                                       conststd::unexpected<E2>& unex);
      (5)(since C++23)

      Performs comparison operations onstd::expected objects.

      1) Compares twostd::expected objects. The objects compare equal if and only if bothlhs andrhs contain expected values that are equal, or both contain unexpected values that are equal.

      If any of the following expressions is ill-formed, or its result is not convertible tobool, the program is ill-formed:

      (until C++26)

      This overload participates in overload resolution only if all following expressions is well-formed, and their results are convertible tobool:

      (since C++26)
      • *lhs==*rhs
      • lhs.error()== rhs.error()
      2) Comparesstd::expected object with anstd::unexpected object. The objects compare equal if and only iflhs contains an unexpected value that is equal tounex.error().

      If the expressionlhs.error()== unex.error() is ill-formed, or its result is not convertible tobool, the program is ill-formed.

      (until C++26)

      This overload participates in overload resolution only if the expressionlhs.error()== unex.error() is well-formed, and its result is convertible tobool.

      (since C++26)
      3) Comparesstd::expected object with an expected value. The objects compare equal if and only iflhs contains an expected value that is equal toval.

      If the expression*lhs== val is ill-formed, or its result is not convertible tobool, the program is ill-formed.

      (until C++26)

      This overload participates in overload resolution only if all following conditions are satisfied:

      • T2 is not a specialization ofstd::expected.
      • The expression*lhs== val is well-formed, and its result is convertible tobool.
      (since C++26)
      4) Compares twostd::expected objects. The objects compare equal if and only iflhs andrhs both represent expected values, or both contain unexpected values that are equal.

      If the expressionlhs.error()== rhs.error() is ill-formed, or its result is not convertible tobool, the program is ill-formed.

      (until C++26)

      This overload participates in overload resolution only if the expressionlhs.error()== rhs.error() is well-formed, and its result is convertible tobool.

      (since C++26)
      5) Comparesstd::expected object with anstd::unexpected object. The objects compare equal if and only iflhs contains an unexpected value that is equal tounex.error().

      If the expressionlhs.error()== unex.error() is ill-formed, or its result is not convertible tobool, the program is ill-formed.

      (until C++26)

      This overload participates in overload resolution only if the expressionlhs.error()== unex.error() is well-formed, and its result is convertible tobool.

      (since C++26)

      These functions are not visible to ordinaryunqualified orqualified lookup, and can only be found byargument-dependent lookup whenstd::expected<T, E> is an associated class of the arguments.

      The!= operator issynthesized fromoperator==.

      Contents

      [edit]Parameters

      lhs, rhs -std::expected object(s) to compare
      unex -std::unexpected value to compare tolhs
      val - value to compare to the expected value contained inlhs

      [edit]Return value

      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())

      [edit]Exceptions

      Throws when and what the comparison throws.

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_constrained_equality202411L(C++26)constrained comparison operators forstd::expected

      [edit]Example

      Run this code
      #include <expected>#include <iostream>#include <string_view> usingnamespace 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";}

      Output:

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

      [edit]See also

      (C++23)
      comparesstd::unexpected objects
      (function template)
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/expected/operator_cmp&oldid=183239"

      [8]ページ先頭

      ©2009-2025 Movatter.jp