operator==,!=,<,<=,>,>=,<=>(std::tuple)
在标头 <tuple> 定义 | ||
template<class... TTypes, class... UTypes> bool operator==(conststd::tuple<TTypes...>& lhs, | (1) | (C++11 起) (C++14 起为 constexpr ) |
template<class... TTypes, class... UTypes> bool operator!=(conststd::tuple<TTypes...>& lhs, | (2) | (C++11 起) (C++14 起为 constexpr )(C++20 前) |
template<class... TTypes, class... UTypes> bool operator<(conststd::tuple<TTypes...>& lhs, | (3) | (C++11 起) (C++14 起为 constexpr )(C++20 前) |
template<class... TTypes, class... UTypes> bool operator<=(conststd::tuple<TTypes...>& lhs, | (4) | (C++11 起) (C++14 起为 constexpr )(C++20 前) |
template<class... TTypes, class... UTypes> bool operator>(conststd::tuple<TTypes...>& lhs, | (5) | (C++11 起) (C++14 起为 constexpr )(C++20 前) |
template<class... TTypes, class... UTypes> bool operator>=(conststd::tuple<TTypes...>& lhs, | (6) | (C++11 起) (C++14 起为 constexpr )(C++20 前) |
template<class... TTypes, class... UTypes> constexprstd::common_comparison_category_t< | (7) | (C++20 起) |
template<class... TTypes, tuple-like UTuple > constexprbool operator==(const tuple<TTypes...>& lhs, const UTuple& rhs ); | (8) | (C++23 起) |
template<class... TTypes, tuple-like UTuple > constexprstd::common_comparison_category_t< | (9) | (C++23 起) |
如果 sizeof...(TTypes) 不等于 sizeof...(UTypes),或者 [ 0, sizeof...(Types)) 中存在 i 使得 std::get<i>(lhs)==std::get<i>(rhs) 不是合法的表达式,那么程序非良构。 如果 [ 0, sizeof...(Types)) 中存在 i 使得 std::get<i>(lhs)==std::get<i>(rhs) 不符合可布尔测试(BooleanTestable) 要求,则行为未定义。 | (C++26 前) |
此重载只有在 sizeof...(TTypes) 等于 sizeof...(UTypes),而且对于 [ 0, sizeof...(Types)) 中的每个 i, std::get<i>(lhs)==std::get<i>(rhs) 为合法的表达式且 decltype(std::get<i>(lhs)==std::get<i>(rhs)) 实现 boolean-testable 时才会参与重载决议。 | (C++26 起) |
if(std::get<0>(lhs)<std::get<0>(rhs))returntrue;
if(std::get<0>(rhs)<std::get<0>(lhs))returnfalse;
if(std::get<1>(lhs)<std::get<1>(rhs))returntrue;
if(std::get<1>(rhs)<std::get<1>(lhs))returnfalse;
...
- 对于空元组,返回 std::strong_ordering::equal。
- 对于非空元组,其效果等价于
if(auto c =
synth-three-way(std::get<0>(lhs), std::get<0>(rhs)); c !=0)return c;
if(auto c =
synth-three-way(std::get<1>(lhs), std::get<1>(rhs)); c !=0)return c;...
return
synth-three-way(std::get<N -1>(lhs), std::get<N -1>(rhs));
tuple-like
对象。/* Elems */ 表示包含 [
0,
std::tuple_size_v<UTuple>)
中按升序的每个 i 对应的类型 std::tuple_element_t<i, UTuple> 的类型包。此重载只能通过实参依赖查找找到。所有比较运算符都是短路的;它们在确定结果所必须的比较之外不会访问其他的元组元素。
| (C++20 起) |
目录 |
[编辑]参数
lhs, rhs | - | 要比较的元组 |
[编辑]返回值
[
0,
sizeof...(Types))
中的 i 都满足 std::get<i>(lhs)==std::get<i>(rhs) 时返回 true,否则返回 false。对两个空元组返回 true。[编辑]注解
各关系运算符是基于各个元素的 operator< 定义的。 | (C++20 前) |
各关系运算符是基于 synth-three-way 定义的,若有可能则它会使用 operator<=>,否则使用 operator<。 要注意,如果元素类型自身并不提供 operator<=>,但可以隐式转换为可三路比较的类型,则就会用这项转换来替代 operator<。 | (C++20 起) |
功能特性测试宏 | 值 | 标准 | 功能特性 |
---|---|---|---|
__cpp_lib_constrained_equality | 202403L | (C++26) | std::tuple 的受约束 operator== |
[编辑]示例
因为 operator< 对于元组有定义,所以可以对元组的容器排序。
#include <algorithm>#include <iostream>#include <tuple>#include <vector> int main(){std::vector<std::tuple<int, std::string, float>> v {{2, "baz", -0.1}, {2, "bar", 3.14}, {1, "foo", 10.1}, {2, "baz", -1.1}, };std::sort(v.begin(), v.end()); for(constauto& p: v)std::cout<<"{ "<< get<0>(p)<<", "<< get<1>(p)<<", "<< get<2>(p)<<" }\n";}
输出:
{ 1, foo, 10.1 } { 2, bar, 3.14 } { 2, baz, -1.1 } { 2, baz, -0.1 }
[编辑]缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 2114 (P2167R3) | C++11 | 缺少布尔运算的类型前条件 | 已添加 |
[编辑]参阅
(C++20 移除)(C++20 移除)(C++20 移除)(C++20 移除)(C++20 移除)(C++20) | 按字典序比较 pair 中的值 (函数模板) |