std::compare_three_way_result
Aus cppreference.com
<tbody>
</tbody>
| definiert in Header <compare>
|
||
template<class T, class U = T> struct compare_three_way_result; |
||
Seien t und u lokalisierbare Werte (lvalues) von const std::remove_reference_t<T> und const std::remove_reference_t<U>, so ist der Klassentyp type zu decltype(t <=> u) definiert, falls der Ausdruck t <=> u wohlgeformt ist. Ansonsten gibt es keinen Klassentype type.
Klassentypen
| Name | Definition |
type
|
the result type of operator<=> on const-qualified lvalue of T and U
|
Hilfstypen
<tbody> </tbody> template<class T, class U = T> using compare_three_way_result_t = typename compare_three_way_result<T, U>::type; |
||
Mögliche Implementierung
// 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>;
};
|
Beispiel
#include <compare>
#include <type_traits>
#include <iostream>
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
Referenzen
(C++20) |
der Ergebnistyp des 3-Wegevergleichs, der alle 6 Operatoren unterstützt, nicht ersetzbar ist und nichtvergleichbare Werte erlaubt (Klasse) |
(C++20) |
das Ergebnis eines 3-Wegevergleichs, der alle 6 Operatoren unterstützt und nicht ersetzbar ist (Klasse) |
(C++20) |
das Ergebnis des 3-Wegevergleichs, der alle 6 Operatoren unterstützt und ersetzbar ist (Klasse) |