std::compare_three_way_result
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
compare_three_way_result
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Relational operators (deprecated in C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
Common vocabulary types
Defined in header
<compare>
template< class T, class U = T >
struct compare_three_way_result;
(since C++20)
struct compare_three_way_result;
Let t
and u
denote lvalue of const std::remove_reference_t <T> and const std::remove_reference_t <U> respectively, if the expression t <=> u is well-formed, provides the member typedef type
equal to decltype(t <=> u), otherwise there is no member type
.
If the program adds specializations for std::compare_three_way_result
, the behavior is undefined.
[edit] Member types
Name
Definition
type
the result type of operator<=> on const-qualified lvalue of T
and U
[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)
using compare_three_way_result_t = compare_three_way_result<T, U>::type;
[edit] Possible implementation
// recommended by Casey Carter // see also: https://github.com/microsoft/STL/pull/385#discussion_r357894054 template<class T, class U = T> using compare_three_way_result_t = decltype( std::declval <const std::remove_reference_t <T>&>() <=> std::declval <const std::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() { if constexpr (std::is_same_v <Ord, std::strong_ordering >) std::cout << "strong ordering\n"; else if constexpr (std::is_same_v <Ord, std::weak_ordering >) std::cout << "weak ordering\n"; else if constexpr (std::is_same_v <Ord, std::partial_ordering >) std::cout << "partial ordering\n"; else std::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 ordering partial ordering
[edit] See also
(C++20)
(class) [edit]
(C++20)
(class) [edit]
(C++20)
(class) [edit]