Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      operator==, !=, <, <=, >, >=, <=>(std::variant)

      From cppreference.com
      <cpp‎ |utility‎ |variant
       
       
      Utilities library
       
       
      Defined in header<variant>
      template<class...Types>

      constexprbool operator==(conststd::variant<Types...>& lhs,

                                 conststd::variant<Types...>& rhs);
      (1)(since C++17)
      template<class...Types>

      constexprbool operator!=(conststd::variant<Types...>& lhs,

                                 conststd::variant<Types...>& rhs);
      (2)(since C++17)
      template<class...Types>

      constexprbool operator<(conststd::variant<Types...>& lhs,

                               conststd::variant<Types...>& rhs);
      (3)(since C++17)
      template<class...Types>

      constexprbool operator>(conststd::variant<Types...>& lhs,

                               conststd::variant<Types...>& rhs);
      (4)(since C++17)
      template<class...Types>

      constexprbool operator<=(conststd::variant<Types...>& lhs,

                                 conststd::variant<Types...>& rhs);
      (5)(since C++17)
      template<class...Types>

      constexprbool operator>=(conststd::variant<Types...>& lhs,

                                 conststd::variant<Types...>& rhs);
      (6)(since C++17)
      template<class...Types>

      constexprstd::common_comparison_category_t
                   <std::compare_three_way_result_t<Types>...>
          operator<=>(conststd::variant<Types...>& lhs,

                       conststd::variant<Types...>& rhs);
      (7)(since C++20)
      Helper function template
      template<std::size_t I,class...Types>

      constexprconststd::variant_alternative_t<I,std::variant<Types...>>&

          GET(const variant<Types...>& v);
      (8)(exposition only*)

      Performs comparison operations onstd::variant objects.

      1-7) Compares twostd::variant objectslhs andrhs. The contained values are compared (using the corresponding operator ofT) only if bothlhs andrhs contain values corresponding to the same index. Otherwise,
      • lhs is consideredequal torhs if, and only if, bothlhs andrhs do not contain a value.
      • lhs is consideredless thanrhs if, and only if, eitherrhs contains a value andlhs does not, orlhs.index() is less thanrhs.index().
      1-6) Let@ denote the corresponding comparison operator, for each of these functions:

      If, for some values ofI, the corresponding expressionGET <I>(lhs) @ GET <I>(rhs) 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 for all values ofI, the corresponding expressionGET <I>(lhs) @ GET <I>(rhs) is well-formed and its result is convertible tobool.

      (since C++26)
      8) The exposition-only function templateGET behaves likestd::get(std::variant), except thatstd::bad_variant_access is never thrown.
      IfI< sizeof...(Types) isfalse, the program is ill-formed.
      IfI== v.index() isfalse, the behavior is undefined.

      Contents

      [edit]Parameters

      lhs,rhs - variants to compare

      [edit]Return value

       Operator Both operands contains a value
      (letI belhs.index() andJ berhs.index())
      lhs orrhs is valueless
      (letlhs_empty belhs.valueless_by_exception() andrhs_empty berhs.valueless_by_exception())
      I andJ are equalI andJ are unequal
      ==GET <I>(lhs)== GET <I>(rhs)falselhs_empty&& rhs_empty
      !=GET <I>(lhs)!= GET <I>(rhs)truelhs_empty!= rhs_empty
      <GET <I>(lhs)< GET <I>(rhs)lhs.index()< rhs.index()lhs_empty&&!rhs_empty
      >GET <I>(lhs)> GET <I>(rhs)lhs.index()> rhs.index()!lhs_empty&& rhs_empty
      <=GET <I>(lhs)<= GET <I>(rhs)lhs.index()< rhs.index()lhs_empty
      >=GET <I>(lhs)>= GET <I>(rhs)lhs.index()> rhs.index()rhs_empty
      <=>GET <I>(lhs)<=> GET <I>(rhs)lhs.index()<=> rhs.index()see below

      Foroperator<=>:

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_constrained_equality202403L(C++26)constrained comparison operators forstd::variant

      [edit]Example

      Run this code
      #include <iostream>#include <string>#include <variant> int main(){std::cout<<std::boolalpha;std::string cmp;bool result; auto print2=[&cmp,&result](constauto& lhs,constauto& rhs){std::cout<< lhs<<' '<< cmp<<' '<< rhs<<" : "<< result<<'\n';}; std::variant<int,std::string> v1, v2; std::cout<<"operator==\n";{        cmp="=="; // by default v1 = 0, v2 = 0;        result= v1== v2;// truestd::visit(print2, v1, v2);         v1= v2=1;        result= v1== v2;// truestd::visit(print2, v1, v2);         v2=2;        result= v1== v2;// falsestd::visit(print2, v1, v2);         v1="A";        result= v1== v2;// false: v1.index == 1, v2.index == 0std::visit(print2, v1, v2);         v2="B";        result= v1== v2;// falsestd::visit(print2, v1, v2);         v2="A";        result= v1== v2;// truestd::visit(print2, v1, v2);} std::cout<<"operator<\n";{        cmp="<";         v1= v2=1;        result= v1< v2;// falsestd::visit(print2, v1, v2);         v2=2;        result= v1< v2;// truestd::visit(print2, v1, v2);         v1=3;        result= v1< v2;// falsestd::visit(print2, v1, v2);         v1="A"; v2=1;        result= v1< v2;// false: v1.index == 1, v2.index == 0std::visit(print2, v1, v2);         v1=1; v2="A";        result= v1< v2;// true: v1.index == 0, v2.index == 1std::visit(print2, v1, v2);         v1= v2="A";        result= v1< v2;// falsestd::visit(print2, v1, v2);         v2="B";        result= v1< v2;// truestd::visit(print2, v1, v2);         v1="C";        result= v1< v2;// falsestd::visit(print2, v1, v2);} {std::variant<int,std::string> v1;std::variant<std::string,int> v2;//  v1 == v2; // Compilation error: no known conversion} // TODO: C++20 three-way comparison operator <=> for variants}

      Output:

      operator==0 == 0 : true1 == 1 : true1 == 2 : falseA == 2 : falseA == B : falseA == A : trueoperator<1 < 1 : false1 < 2 : true3 < 2 : falseA < 1 : false1 < A : trueA < A : falseA < B : trueC < B : false

      [edit]See also

      (C++17)(C++17)(C++17)(C++17)(C++17)(C++17)(C++20)
      comparesoptional objects
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant/operator_cmp&oldid=179395"

      [8]ページ先頭

      ©2009-2025 Movatter.jp