std::compare_three_way_result
來自 cppreference.com
定義於標頭檔案 <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 是良構的,則提供成員 typedef type
等於 decltype(t <=> u),否則沒有成員 type
。
如果程式為 std::compare_three_way_result
新增特化,則行為是未定義的。
目錄 |
[編輯] 成員型別
名稱 | 定義 |
型別
|
對 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 起) | |
[編輯] 可能的實現
// 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>; }; |
[編輯] 示例
執行此程式碼
#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個運算子並且可替換的三路比較的結果型別 (類) |