std::compare_weak_order_fallback
定义于头文件 <compare> |
||
inline namespace /* unspecified */ { inline constexpr /* unspecified */ |
(自 C++20 起) | |
调用签名 |
||
template< class T, class U > requires /* see below */ |
(自 C++20 起) | |
对子表达式 t 和 u 执行三路比较,并生成 std::weak_ordering 类型的结果,即使运算符 <=>
不可用。
如果 std::decay_t<T> 和 std::decay_t<U> 是相同类型,则 std::compare_weak_order_fallback(t, u) 表达式等价于
- std::weak_order(t, u),如果它是良构表达式;否则,
- t == u ? std::weak_ordering::equivalent :
t < u ? std::weak_ordering::less :
std::weak_ordering::greater,如果表达式 t == u 和 t < u 都是良构的,并且 decltype(t == u) 和 decltype(t < u) 都符合 boolean-testable 模型,除了 t 和 u 只求值一次。
在所有其他情况下,std::compare_weak_order_fallback(t, u) 是非良构的,当它出现在模板实例化的直接上下文中时,可能导致替换失败。
目录 |
自定义点对象
名称 std::compare_weak_order_fallback
表示一个自定义点对象,它是一个 const 函数对象,属于 字面量 semiregular
类类型。为了说明目的,其类型的 cv-非限定版本表示为 __compare_weak_order_fallback_fn
。
__compare_weak_order_fallback_fn
的所有实例均相等。在相同参数上调用 __compare_weak_order_fallback_fn
类型的不同实例的效果是等效的,无论表示实例的表达式是左值还是右值,以及是否具有 const 限定符(但是,不要求可调用 volatile 限定的实例)。因此,std::compare_weak_order_fallback
可以自由复制,并且其副本可以互换使用。
给定一组类型 Args...
,如果 std::declval<Args>()... 满足上述 std::compare_weak_order_fallback
参数的要求,则 __compare_weak_order_fallback_fn
建模为
- std::invocable<__compare_weak_order_fallback_fn, Args...>,
- std::invocable<const __compare_weak_order_fallback_fn, Args...>,
- std::invocable<__compare_weak_order_fallback_fn&, Args...>,以及
- std::invocable<const __compare_weak_order_fallback_fn&, Args...>.
否则,__compare_weak_order_fallback_fn
的函数调用运算符均不参与重载解析。
[编辑] 示例
#include <compare> #include <iostream> // does not support <=> struct Rational_1 { int num; int den; // > 0 }; inline constexpr bool operator<(Rational_1 lhs, Rational_1 rhs) { return lhs.num * rhs.den < rhs.num * lhs.den; } inline constexpr bool operator==(Rational_1 lhs, Rational_1 rhs) { return lhs.num * rhs.den == rhs.num * lhs.den; } // supports <=> struct Rational_2 { int num; int den; // > 0 }; inline constexpr std::weak_ordering operator<=>(Rational_2 lhs, Rational_2 rhs) { return lhs.num * rhs.den <=> rhs.num * lhs.den; } inline constexpr bool operator==(Rational_2 lhs, Rational_2 rhs) { return lhs <=> rhs == 0; } void print(int id, std::weak_ordering value) { std::cout << id << ") "; if (value == 0) std::cout << "equal\n"; else if (value < 0) std::cout << "less\n"; else std::cout << "greater\n"; } int main() { Rational_1 a{1, 2}, b{3, 4}; // print(0, a <=> b); // does not work print(1, std::compare_weak_order_fallback(a, b)); // works, defaults to < and == Rational_2 c{6, 5}, d{8, 7}; print(2, c <=> d); // works print(3, std::compare_weak_order_fallback(c, d)); // works Rational_2 e{2, 3}, f{4, 6}; print(4, e <=> f); // works print(5, std::compare_weak_order_fallback(e, f)); // works }
输出
1) less 2) greater 3) greater 4) equal 5) equal
[编辑] 缺陷报告
以下行为更改缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2114 (P2167R3) |
C++20 | 回退机制仅要求 返回类型可转换为 bool |
约束已加强 |
[编辑] 参见
(C++20) |
执行三路比较并生成 std::weak_ordering 类型的结果(自定义点对象) |