标准库标头 <compare> (C++20)
来自cppreference.com
此头文件是语言支持库的一部分。
概念 | ||
指定运算符 <=> 在给定类型上产生一致的结果 (概念) | ||
类 | ||
(C++20) | 三路比较的结果类型,支持所有 6 种运算符且不可替换,并允许不可比较的值 (类) | |
(C++20) | 三路比较的结果类型,支持所有 6 种运算符且不可替换 (类) | |
(C++20) | 三路比较的结果类型,支持所有 6 种运算符且可替换 (类) | |
(C++20) | 给定的全部类型都能转换到的最强比较类别 (类模板) | |
(C++20) | 获得三路比较运算符 <=> 在给定类型上的结果 (类模板) | |
(C++20) | 实现 x <=> y 的受约束函数对象 (类) | |
定制点对象 | ||
(C++20) | 进行三路比较并产生 std::strong_ordering 类型的结果 (定制点对象) | |
(C++20) | 进行三路比较并产生 std::weak_ordering 类型的结果 (定制点对象) | |
(C++20) | 进行三路比较并产生 std::partial_ordering 类型的结果 (定制点对象) | |
进行三路比较并产生 std::strong_ordering 类型的结果,即使 operator<=> 不可用 (定制点对象) | ||
(C++20) | 进行三路比较并产生 std::weak_ordering 类型的结果,即使 operator<=> 不可用 (定制点对象) | |
进行三路比较并产生 std::partial_ordering 类型的结果,即使 operator<=> 不可用 (定制点对象) | ||
函数 | ||
具名比较函数 (函数) |
[编辑]概要
namespace std {// 比较类别类型class partial_ordering;class weak_ordering;class strong_ordering; // 具名比较函数constexprbool is_eq (partial_ordering cmp)noexcept{return cmp ==0;}constexprbool is_neq (partial_ordering cmp)noexcept{return cmp !=0;}constexprbool is_lt (partial_ordering cmp)noexcept{return cmp <0;}constexprbool is_lteq(partial_ordering cmp)noexcept{return cmp <=0;}constexprbool is_gt (partial_ordering cmp)noexcept{return cmp >0;}constexprbool is_gteq(partial_ordering cmp)noexcept{return cmp >=0;} // 公共比较类别类型template<class... Ts>struct common_comparison_category {using type =/* 见描述 */;};template<class... Ts>using common_comparison_category_t =typename common_comparison_category<Ts...>::type; // 概念 three_way_comparabletemplate<class T, class Cat = partial_ordering> concept three_way_comparable =/* 见描述 */;template<class T, class U, class Cat = partial_ordering> concept three_way_comparable_with =/* 见描述 */; // 三路比较的结果template<class T, class U = T>struct compare_three_way_result; template<class T, class U = T>using compare_three_way_result_t =typename compare_three_way_result<T, U>::type; // 类 compare_three_waystruct compare_three_way; // 比较算法inlinenamespace/* 未指明 */{inlineconstexpr/* 未指明 */ strong_order =/* 未指明 */;inlineconstexpr/* 未指明 */ weak_order =/* 未指明 */;inlineconstexpr/* 未指明 */ partial_order =/* 未指明 */;inlineconstexpr/* 未指明 */ compare_strong_order_fallback =/* 未指明 */;inlineconstexpr/* 未指明 */ compare_weak_order_fallback =/* 未指明 */;inlineconstexpr/* 未指明 */ compare_partial_order_fallback =/* 未指明 */;}}
[编辑]概念 three_way_comparable
namespace std {template<class T, class Cat> concept __ComparesAs =// 仅用于阐释 same_as<common_comparison_category_t<T, Cat>, Cat>; template<class T, class U> concept __PartiallyOrderedWith =// 仅用于阐释 requires(const remove_reference_t<T>& t, const remove_reference_t<U>& u){{ t < u }->boolean-testable;{ t > u }->boolean-testable;{ t <= u }->boolean-testable;{ t >= u }->boolean-testable;{ u < t }->boolean-testable;{ u > t }->boolean-testable;{ u <= t }->boolean-testable;{ u >= t }->boolean-testable;}; template<class T, class Cat = partial_ordering> concept three_way_comparable = __WeaklyEqualityComparableWith<T, T>&& __PartiallyOrderedWith<T, T>&& requires(const remove_reference_t<T>& a, const remove_reference_t<T>& b){{ a <=> b }-> __ComparesAs<Cat>;};}
[编辑]概念 three_way_comparable_with
namespace std {template<class T, class U, class Cat = partial_ordering> concept three_way_comparable_with = __WeaklyEqualityComparableWith<T, U>&& __PartiallyOrderedWith<T, U>&& three_way_comparable<T, Cat>&& three_way_comparable<U, Cat>&& common_reference_with<const remove_reference_t<T>&, const remove_reference_t<U>&>&& three_way_comparable< common_reference_t<const remove_reference_t<T>&, const remove_reference_t<U>&>, Cat>&& requires(const remove_reference_t<T>& t, const remove_reference_t<U>& u){{ t <=> u }-> __ComparesAs<Cat>;{ u <=> t }-> __ComparesAs<Cat>;};}
[编辑]类 std::partial_ordering
namespace std {class partial_ordering {int value;// 仅用于阐释bool is_ordered;// 仅用于阐释 // 仅用于阐释的构造函数constexprexplicit partial_ordering(eq v)noexcept: value(int(v)), is_ordered(true){}// 仅用于阐释constexprexplicit partial_ordering(ord v)noexcept: value(int(v)), is_ordered(true){}// 仅用于阐释constexprexplicit partial_ordering(ncmp v)noexcept: value(int(v)), is_ordered(false){}// 仅用于阐释 public:// 合法值staticconst partial_ordering less;staticconst partial_ordering equivalent;staticconst partial_ordering greater;staticconst partial_ordering unordered; // 比较friendconstexprbool operator==(partial_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator==(partial_ordering v, partial_ordering w)noexcept=default;friendconstexprbool operator<(partial_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator>(partial_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator<=(partial_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator>=(partial_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator<(/* 未指明 */, partial_ordering v)noexcept;friendconstexprbool operator>(/* 未指明 */, partial_ordering v)noexcept;friendconstexprbool operator<=(/* 未指明 */, partial_ordering v)noexcept;friendconstexprbool operator>=(/* 未指明 */, partial_ordering v)noexcept;friendconstexpr partial_ordering operator<=>(partial_ordering v, /* 未指明 */)noexcept;friendconstexpr partial_ordering operator<=>(/* 未指明 */, partial_ordering v)noexcept;}; // 合法值的定义inlineconstexpr partial_ordering partial_ordering::less(ord::less);inlineconstexpr partial_ordering partial_ordering::equivalent(eq::equivalent);inlineconstexpr partial_ordering partial_ordering::greater(ord::greater);inlineconstexpr partial_ordering partial_ordering::unordered(ncmp::unordered);}
[编辑]类 std::weak_ordering
namespace std {class weak_ordering {int value;// 仅用于阐释 // 仅用于阐释的构造函数constexprexplicit weak_ordering(eq v)noexcept: value(int(v)){}// 仅用于阐释constexprexplicit weak_ordering(ord v)noexcept: value(int(v)){}// 仅用于阐释 public:// 合法值staticconst weak_ordering less;staticconst weak_ordering equivalent;staticconst weak_ordering greater; // 转换constexpr operator partial_ordering()constnoexcept; // 比较friendconstexprbool operator==(weak_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator==(weak_ordering v, weak_ordering w)noexcept=default;friendconstexprbool operator<(weak_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator>(weak_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator<=(weak_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator>=(weak_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator<(/* 未指明 */, weak_ordering v)noexcept;friendconstexprbool operator>(/* 未指明 */, weak_ordering v)noexcept;friendconstexprbool operator<=(/* 未指明 */, weak_ordering v)noexcept;friendconstexprbool operator>=(/* 未指明 */, weak_ordering v)noexcept;friendconstexpr weak_ordering operator<=>(weak_ordering v, /* 未指明 */)noexcept;friendconstexpr weak_ordering operator<=>(/* 未指明 */, weak_ordering v)noexcept;}; // 合法值的定义inlineconstexpr weak_ordering weak_ordering::less(ord::less);inlineconstexpr weak_ordering weak_ordering::equivalent(eq::equivalent);inlineconstexpr weak_ordering weak_ordering::greater(ord::greater);}
[编辑]类 std::strong_ordering
namespace std {class strong_ordering {int value;// 仅用于阐释 // 仅用于阐释的构造函数constexprexplicit strong_ordering(eq v)noexcept: value(int(v)){}// 仅用于阐释constexprexplicit strong_ordering(ord v)noexcept: value(int(v)){}// 仅用于阐释 public:// 合法值staticconst strong_ordering less;staticconst strong_ordering equal;staticconst strong_ordering equivalent;staticconst strong_ordering greater; // 转换constexpr operator partial_ordering()constnoexcept;constexpr operator weak_ordering()constnoexcept; // 比较friendconstexprbool operator==(strong_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator==(strong_ordering v, strong_ordering w)noexcept=default;friendconstexprbool operator<(strong_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator>(strong_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator<=(strong_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator>=(strong_ordering v, /* 未指明 */)noexcept;friendconstexprbool operator<(/* 未指明 */, strong_ordering v)noexcept;friendconstexprbool operator>(/* 未指明 */, strong_ordering v)noexcept;friendconstexprbool operator<=(/* 未指明 */, strong_ordering v)noexcept;friendconstexprbool operator>=(/* 未指明 */, strong_ordering v)noexcept;friendconstexpr strong_ordering operator<=>(strong_ordering v, /* 未指明 */)noexcept;friendconstexpr strong_ordering operator<=>(/* 未指明 */, strong_ordering v)noexcept;}; // 合法值的定义inlineconstexpr strong_ordering strong_ordering::less(ord::less);inlineconstexpr strong_ordering strong_ordering::equal(eq::equal);inlineconstexpr strong_ordering strong_ordering::equivalent(eq::equivalent);inlineconstexpr strong_ordering strong_ordering::greater(ord::greater);}
[编辑]类 std::compare_three_way
namespace std {struct compare_three_way {template<class T, class U>constexprauto operator()(T&& t, U&& u)const; using is_transparent =/* 未指明 */;};}
[编辑]参阅
三路比较运算符 表达式 lhs<=> rhs(C++20) |