operator==,!=,<,<=,>,>=,<=> (std::tuple)
|
定义于头文件
<tuple>
|
||
|
template
<
class
...
TTypes
,
class
...
UTypes
>
bool
operator
==
(
const
std::
tuple
<
TTypes...
>
&
lhs,
|
(1) |
(C++11 起)
(C++14 起为 constexpr) |
|
template
<
class
...
TTypes
,
class
...
UTypes
>
bool
operator
!
=
(
const
std::
tuple
<
TTypes...
>
&
lhs,
|
(2) |
(C++11 起)
(C++14 起为 constexpr) (C++20 前) |
|
template
<
class
...
TTypes
,
class
...
UTypes
>
bool
operator
<
(
const
std::
tuple
<
TTypes...
>
&
lhs,
|
(3) |
(C++11 起)
(C++14 起为 constexpr) (C++20 前) |
|
template
<
class
...
TTypes
,
class
...
UTypes
>
bool
operator
<=
(
const
std::
tuple
<
TTypes...
>
&
lhs,
|
(4) |
(C++11 起)
(C++14 起为 constexpr) (C++20 前) |
|
template
<
class
...
TTypes
,
class
...
UTypes
>
bool
operator
>
(
const
std::
tuple
<
TTypes...
>
&
lhs,
|
(5) |
(C++11 起)
(C++14 起为 constexpr) (C++20 前) |
|
template
<
class
...
TTypes
,
class
...
UTypes
>
bool
operator
>=
(
const
std::
tuple
<
TTypes...
>
&
lhs,
|
(6) |
(C++11 起)
(C++14 起为 constexpr) (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
)
,或对于任意
i
在
[
0
,
sizeof...
(
Types
)
)
范围内,
std
::
get
<
i
>
(
lhs
)
==
std
::
get
<
i
>
(
rhs
)
不是合法表达式,则程序非良构。
若对于任意
i
在
[
0
,
sizeof...
(
Types
)
)
范围内,
std
::
get
<
i
>
(
lhs
)
==
std
::
get
<
i
>
(
rhs
)
的类型与值类别不满足
BooleanTestable
要求,则行为未定义。
|
(C++26 前) |
|
此重载仅当
sizeof...
(
TTypes
)
等于
sizeof...
(
UTypes
)
,且对于每个
i
在
[
0
,
sizeof...
(
Types
)
)
范围内,
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 ) ) 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
对象,且
rhs
的元素数量由
std::
tuple_size_v
<
UTuple
>
确定。此重载只能通过
实参依赖查找
找到。
tuple-like
对象。
/* Elems */
表示类型包
std::
tuple_element_t
<
i, UTuple
>
,其中
i
在区间
[
0
,
std::
tuple_size_v
<
UTuple
>
)
内按递增顺序遍历。此重载只能通过
实参依赖查找
被发现。
所有比较运算符都是短路的;它们不会访问超出确定比较结果所需的元组元素。
|
|
(since C++20) |
目录 |
参数
| lhs, rhs | - | 待比较的元组 |
返回值
[
0
,
sizeof...
(
Types
)
)
范围内的
i
都满足
std
::
get
<
i
>
(
lhs
)
==
std
::
get
<
i
>
(
rhs
)
,否则返回
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 (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++ 标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确行为 |
|---|---|---|---|
|
LWG 2114
( P2167R3 ) |
C++11 | 布尔操作的类型前置条件缺失 | 已添加 |
参见
|
(C++20 中移除)
(C++20 中移除)
(C++20 中移除)
(C++20 中移除)
(C++20 中移除)
(C++20)
|
按字典序比较
pair
中的值
(函数模板) |