Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::compare_three_way_result

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
       
      Defined in header<compare>
      template<class T,class U= T>
      struct compare_three_way_result;
      (since C++20)

      Lett andu denote lvalue ofconststd::remove_reference_t<T> andconststd::remove_reference_t<U> respectively, if the expressiont<=> u is well-formed, provides the member typedeftype equal todecltype(t<=> u), otherwise there is no membertype.

      If the program adds specializations forstd::compare_three_way_result, the behavior is undefined.

      Contents

      [edit]Member types

      Name Definition
      type the result type ofoperator<=> on const-qualified lvalue ofT andU

      [edit]Helper types

      template<class T,class U= T>
      using compare_three_way_result_t= compare_three_way_result<T, U>::type;
      (since C++20)

      [edit]Possible implementation

      // recommended by Casey Carter// see also: https://github.com/microsoft/STL/pull/385#discussion_r357894054template<class T,class U= T>using compare_three_way_result_t= decltype(std::declval<conststd::remove_reference_t<T>&>()<=>std::declval<conststd::remove_reference_t<U>&>()); template<class T,class U= T>struct compare_three_way_result{}; template<class T,class U>    requires requires{typename compare_three_way_result_t<T, U>;}struct compare_three_way_result<T, U>{using type= compare_three_way_result_t<T, U>;};

      [edit]Example

      Run this code
      #include <compare>#include <iostream>#include <type_traits> template<class Ord>void print_cmp_type(){ifconstexpr(std::is_same_v<Ord,std::strong_ordering>)std::cout<<"strong ordering\n";elseifconstexpr(std::is_same_v<Ord,std::weak_ordering>)std::cout<<"weak ordering\n";elseifconstexpr(std::is_same_v<Ord,std::partial_ordering>)std::cout<<"partial ordering\n";elsestd::cout<<"illegal comparison result type\n";} int main(){    print_cmp_type<std::compare_three_way_result_t<int>>();    print_cmp_type<std::compare_three_way_result_t<double>>();}

      Output:

      strong orderingpartial ordering

      [edit]See also

      the result type of 3-way comparison that supports all 6 operators, is not substitutable, and allows incomparable values
      (class)[edit]
      the result type of 3-way comparison that supports all 6 operators and is not substitutable
      (class)[edit]
      the result type of 3-way comparison that supports all 6 operators and is substitutable
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/compare/compare_three_way_result&oldid=170276"

      [8]ページ先頭

      ©2009-2025 Movatter.jp