Namespaces
Variants

std:: compare_three_way_result

From cppreference.net
Utilities library
ヘッダーで定義 <compare>
template < class T, class U = T >
struct compare_three_way_result ;
(C++20以降)

t u をそれぞれ const std:: remove_reference_t < T > const std:: remove_reference_t < U > の左辺値とする。式 t <=> u が適正に形成される場合、メンバ型 type decltype ( t <=> u ) と等しく定義する。そうでない場合、メンバ type は存在しない。

プログラムが std::compare_three_way_result に対する特殊化を追加する場合、動作は未定義です。

目次

メンバー型

名前 定義
type const修飾された T U の左辺値に対する operator <=> の結果型

ヘルパー型

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

実装例

// Casey Carterによって推奨
// 参照: 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>;
};

#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>>();
}

出力:

strong ordering
partial ordering

関連項目

すべての6つの演算子をサポートし、置換可能ではなく、比較不能な値を許容する3方向比較の結果型
(クラス)
すべての6つの演算子をサポートし、置換可能ではない3方向比較の結果型
(クラス)
すべての6つの演算子をサポートし、置換可能な3方向比較の結果型
(クラス)