Namespaces
Variants

std:: common_comparison_category

From cppreference.net
Utilities library
定义于头文件 <compare>
template < class ... Ts >

struct common_comparison_category
{
using type = /* 见下文 */ ;

} ;
(C++20 起)

类模板 std::common_comparison_category 提供一个别名(作为成员类型定义 type ),表示所有模板参数 Ts... 都能转换到的最强比较类别。

具体而言,由 n 个类型 T 0 ... T n-1 组成的列表的公共比较类型定义如下:

目录

模板参数

...Ts - 可能为空的类型列表

辅助模板

template < class ... Ts >
using common_comparison_category_t = common_comparison_category < Ts... > :: type ;
(C++20 起)

成员类型

成员类型 定义
type 最强的通用比较类别(按上述定义)

可能的实现

namespace detail
{
    template<unsigned int>
    struct common_cmpcat_base     { using type = void; };
    template<>
    struct common_cmpcat_base<0u> { using type = std::strong_ordering; };
    template<>
    struct common_cmpcat_base<2u> { using type = std::partial_ordering; };
    template<>
    struct common_cmpcat_base<4u> { using type = std::weak_ordering; };
    template<>
    struct common_cmpcat_base<6u> { using type = std::partial_ordering; };
} // 命名空间 detail
template<class...Ts>
struct common_comparison_category :
    detail::common_cmpcat_base<(0u | ... |
        (std::is_same_v<Ts, std::strong_ordering>  ? 0u :
         std::is_same_v<Ts, std::weak_ordering>    ? 4u :
         std::is_same_v<Ts, std::partial_ordering> ? 2u : 1u)
    )> {};

示例

另请参阅

支持全部6种运算符且可替换的三路比较结果类型
(类)
支持全部6种运算符但不可替换的三路比较结果类型
(类)
支持全部6种运算符、不可替换且允许不可比较值的三路比较结果类型
(类)