std::compare_three_way_result
ja.cppreference.net より
| ヘッダ <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 が well-formed である場合、メンバ型 typedef 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
関連項目
|
(C++20)
|
すべての6つの演算子をサポートし、置換可能ではなく、比較不能な値を許容する3方向比較の結果型
(クラス) |
|
(C++20)
|
すべての6つの演算子をサポートし、置換可能ではない3方向比較の結果型
(クラス) |
|
(C++20)
|
すべての6つの演算子をサポートし、置換可能な3方向比較の結果型
(クラス) |