std::compare_three_way_result
提供: cppreference.com
ヘッダ <compare> で定義 | ||
template<class T, class U = T> struct compare_three_way_result; | (C++20以上) | |
t
と u
がそれぞれ conststd::remove_reference_t<T> と conststd::remove_reference_t<U> の左辺値を表すとき、式 t <=> u が well-formed であれば、 decltype(t <=> u) に等しいメンバ型 type
を提供します。 そうでなければ、メンバ type
は存在しません。
このテンプレートを特殊化する試みは未定義動作です。
目次 |
[編集]メンバ型
名前 | 定義 |
type | T と U の const 修飾された左辺値に対する operator<=> の結果の型 |
[編集]ヘルパー型
template<class T, class U = T> using compare_three_way_result_t =typename compare_three_way_result<T>::type; | (C++20以上) | |
[編集]実装例
template<class T, class U = T>struct compare_three_way_result {}; template<class T, class U> requires requires(conststd::remove_reference_t<T>& t, conststd::remove_reference_t<U>& u){ t <=> u;}struct compare_three_way_result<T, U>{using type = decltype(std::declval<conststd::remove_reference_t<T>&>()<=>std::declval<conststd::remove_reference_t<U>&>());}; |
[編集]例
Run this code
#include <compare>#include <type_traits>#include <iostream> template<class Ord>void print_cmp_type(){ifconstexpr(std::is_same_v<Ord, std::strong_ordering>)std::cout<<"strong ordering\n";elseifconstexpr(std::is_same_v<Ord, std::weak_ordering>)std::cout<<"weak ordering\n";elseifconstexpr(std::is_same_v<Ord, std::partial_ordering>)std::cout<<"partial ordering\n";elseifconstexpr(std::is_same_v<Ord, std::strong_equality>)std::cout<<"strong equality\n";elseifconstexpr(std::is_same_v<Ord, std::weak_equality>)std::cout<<"weak equality\n";elsestd::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>>(); print_cmp_type<std::compare_three_way_result_t<int(*)()>>();}
出力:
strong ordering partial ordering strong equality
[編集]関連項目
(C++20) | 等しい、等しくないのみサポートする代用可能でない三方比較の結果の型 (クラス) |
(C++20) | 等しい、等しくないのみサポートする代用可能な三方比較の結果の型 (クラス) |
(C++20) | 6種類の演算子をすべてサポートし、代用可能でなく、比較不可能な値を許容する、三方比較の結果の型 (クラス) |
(C++20) | 6種類の演算子をすべてサポートする代用可能でない三方比較の結果の型 (クラス) |
(C++20) | 6種類の演算子をすべてサポートする代用可能な三方比較の結果の型 (クラス) |