operator==,!=,<,<=,>,>=,<=>(std::tuple)
定义于头文件 <tuple> |
||
template< class... TTypes, class... UTypes > bool operator==( const std::tuple<TTypes...>& lhs, |
(1) | (自 C++11 起) (constexpr 自 C++14 起) |
template< class... TTypes, class... UTypes > bool operator!=( const std::tuple<TTypes...>& lhs, |
(2) | (自 C++11 起) (constexpr 自 C++14 起) (直到 C++20) |
template< class... TTypes, class... UTypes > bool operator<( const std::tuple<TTypes...>& lhs, |
(3) | (自 C++11 起) (constexpr 自 C++14 起) (直到 C++20) |
template< class... TTypes, class... UTypes > bool operator<=( const std::tuple<TTypes...>& lhs, |
(4) | (自 C++11 起) (constexpr 自 C++14 起) (直到 C++20) |
template< class... TTypes, class... UTypes > bool operator>( const std::tuple<TTypes...>& lhs, |
(5) | (自 C++11 起) (constexpr 自 C++14 起) (直到 C++20) |
template< class... TTypes, class... UTypes > bool operator>=( const std::tuple<TTypes...>& lhs, |
(6) | (自 C++11 起) (constexpr 自 C++14 起) (直到 C++20) |
template< class... TTypes, class... UTypes > constexpr std::common_comparison_category_t< |
(7) | (自 C++20 起) |
template< class... TTypes, tuple-like UTuple > constexpr bool operator==( const tuple<TTypes...>& lhs, const UTuple& rhs ); |
(8) | (自 C++23 起) |
template< class... TTypes, tuple-like UTuple > constexpr std::common_comparison_category_t< |
(9) | (自 C++23 起) |
如果 sizeof...(TTypes) 不等于 sizeof...(UTypes),或者 std::get<i>(lhs) == std::get<i>(rhs) 对于
[ 0, sizeof...(Types)) 中的任何 i 都不是有效表达式,则程序是非良构的。 如果 std::get<i>(lhs) == std::get<i>(rhs) 的类型和值类别不满足 BooleanTestable 要求,对于 [ 0, sizeof...(Types)) 中的任何 i,则行为未定义。 |
(直到 C++26) |
仅当 sizeof...(TTypes) 等于 sizeof...(UTypes),std::get<i>(lhs) == std::get<i>(rhs) 是有效表达式,并且 decltype(std::get<i>(lhs) == std::get<i>(rhs)) 为每个 [ 0, sizeof...(Types)) 中的 i 建模 boolean-testable 时,此重载才参与重载决议。 |
(自 C++26 起) |
if (std::get<0>(lhs) < std::get<0>(rhs)) return true;
if (std::get<0>(rhs) < std::get<0>(lhs)) return false;
if (std::get<1>(lhs) < std::get<1>(rhs)) return true;
if (std::get<1>(rhs) < std::get<1>(lhs)) return false;
...
- 对于空元组,返回 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 */ 表示类型包 std::tuple_element_t<i, UTuple>,对于 [
0,
std::tuple_size_v<UTuple>)
中的每个 i,按递增顺序排列。此重载只能通过 实参依赖查找 找到。所有比较运算符都是短路求值;它们不会访问超出确定比较结果所需的元组元素。
|
(自 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 起) |
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__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 (const auto& 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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2114 (P2167R3) |
C++11 | 布尔运算的类型前提条件缺失 | 已添加 |
[编辑] 参见
(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(C++20) |
字典序比较 pair 中的值(函数模板) |