Namespaces
Variants

std:: compare_three_way_result

From cppreference.net
Utilities library
Definiert im Header <compare>
template < class T, class U = T >
struct compare_three_way_result ;
(seit C++20)

Seien t und u Lvalues von const std:: remove_reference_t < T > bzw. const std:: remove_reference_t < U > , falls der Ausdruck t <=> u wohlgeformt ist, stellt den Member-Typ type bereit, gleich decltype ( t <=> u ) , andernfalls existiert kein Member type .

Wenn das Programm Spezialisierungen für std::compare_three_way_result hinzufügt, ist das Verhalten undefiniert.

Inhaltsverzeichnis

Mitgliedertypen

Name Definition
type der Ergebnistyp von operator <=> auf const-qualifizierten Lvalues von T und U

Hilfstypen

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

Mögliche Implementierung

// empfohlen von Casey Carter
// siehe auch: 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 <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>>();
}

Ausgabe:

strong ordering
partial ordering

Siehe auch

der Ergebnistyp des 3-Wege-Vergleichs, der alle 6 Operatoren unterstützt, nicht substituierbar ist und unvergleichbare Werte erlaubt
(Klasse)
der Ergebnistyp des 3-Wege-Vergleichs, der alle 6 Operatoren unterstützt und nicht substituierbar ist
(Klasse)
der Ergebnistyp des 3-Wege-Vergleichs, der alle 6 Operatoren unterstützt und substituierbar ist
(Klasse)