std:: compare_three_way_result
|
定义于头文件
<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
合法,则提供等于
decltype
(
t
<=>
u
)
的成员 typedef
type
,否则不存在成员
type
。
如果程序为
std::compare_three_way_result
添加特化,则行为未定义。
目录 |
成员类型
| 名称 | 定义 |
type
|
对
T
和
U
的 const 限定左值进行
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
参见
|
(C++20)
|
支持全部6种运算符、不可替换且允许不可比较值的三路比较结果类型
(类) |
|
(C++20)
|
支持全部6种运算符且不可替换的三路比较结果类型
(类) |
|
(C++20)
|
支持全部6种运算符且可替换的三路比较结果类型
(类) |