Namespaces
Variants

operator==,!=,<,<=,>,>=,<=> (std::tuple)

From cppreference.net
Utilities library
定义于头文件 <tuple>
template < class ... TTypes , class ... UTypes >

bool operator == ( const std:: tuple < TTypes... > & lhs,

const std:: tuple < UTypes... > & rhs ) ;
(1) (C++11 起)
(C++14 起为 constexpr)
template < class ... TTypes , class ... UTypes >

bool operator ! = ( const std:: tuple < TTypes... > & lhs,

const std:: tuple < UTypes... > & rhs ) ;
(2) (C++11 起)
(C++14 起为 constexpr)
(C++20 前)
template < class ... TTypes , class ... UTypes >

bool operator < ( const std:: tuple < TTypes... > & lhs,

const std:: tuple < UTypes... > & rhs ) ;
(3) (C++11 起)
(C++14 起为 constexpr)
(C++20 前)
template < class ... TTypes , class ... UTypes >

bool operator <= ( const std:: tuple < TTypes... > & lhs,

const std:: tuple < UTypes... > & rhs ) ;
(4) (C++11 起)
(C++14 起为 constexpr)
(C++20 前)
template < class ... TTypes , class ... UTypes >

bool operator > ( const std:: tuple < TTypes... > & lhs,

const std:: tuple < UTypes... > & rhs ) ;
(5) (C++11 起)
(C++14 起为 constexpr)
(C++20 前)
template < class ... TTypes , class ... UTypes >

bool operator >= ( const std:: tuple < TTypes... > & lhs,

const std:: tuple < UTypes... > & rhs ) ;
(6) (C++11 起)
(C++14 起为 constexpr)
(C++20 前)
template < class ... TTypes , class ... UTypes >

constexpr std:: common_comparison_category_t <
synth - three - way - result < TTypes, Elems > ... >
operator <=> ( const std:: tuple < TTypes... > & lhs,

const std:: tuple < UTypes... > & rhs ) ;
(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 <
synth - three - way - result < TTypes, /* Elems */ > ... >

operator <=> ( const tuple < TTypes... > & lhs, const UTuple & rhs ) ;
(9) (C++23 起)
1,2) 将元组 lhs 的每个元素与元组 rhs 的对应元素通过 operator == 进行比较。
1) 若所有对应元素对都相等则返回 true
2) 返回 ! ( lhs == rhs )
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 起)
3-6) 通过 operator < lhs rhs 进行字典序比较,即先比较首元素,若等价则比较次元素,若仍等价则继续比较后续元素,依此类推。
3) 对于空元组,返回 false 。对于非空元组,效果等同于
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 ;
...

return std :: get < N - 1 > ( lhs ) < std :: get < N - 1 > ( rhs ) ;
4) 返回 ! ( rhs < lhs )
5) 返回 rhs < lhs
6) 返回 ! ( lhs < rhs )
如果 sizeof... ( TTypes ) 不等于 sizeof... ( UTypes ) ,或等效声明中所示的任何比较表达式不是有效表达式,则程序非良构。
若等价语句中所示的任何比较表达式的类型和值类别不满足 BooleanTestable 要求,则行为未定义。
7) 通过 synth-three-way lhs rhs 进行字典序比较,即先比较首元素,若等价则比较次元素,若仍等价则继续比较后续元素,依此类推。

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 ) ) ;

8) (1) 相同,区别在于 rhs 是一个 tuple-like 对象,且 rhs 的元素数量由 std:: tuple_size_v < UTuple > 确定。此重载只能通过 实参依赖查找 找到。
9) (7) 相同,区别在于 rhs 是一个 tuple-like 对象。 /* Elems */ 表示类型包 std:: tuple_element_t < i, UTuple > ,其中 i 在区间 [ 0 , std:: tuple_size_v < UTuple > ) 内按递增顺序遍历。此重载只能通过 实参依赖查找 被发现。

所有比较运算符都是短路的;它们不会访问超出确定比较结果所需的元组元素。

< <= > >= != 运算符分别由 operator <=> operator == 自动合成。

(since C++20)

目录

参数

lhs, rhs - 待比较的元组

返回值

1,8) true 当且仅当对于所有在 [ 0 , sizeof... ( Types ) ) 范围内的 i 都满足 std :: get < i > ( lhs ) == std :: get < i > ( rhs ) ,否则返回 false 。对于两个空元组始终返回 true
2) ! ( lhs == rhs )
3) true lhs 中首个不相等元素小于 rhs 中的对应元素时; false rhs 中首个不相等元素小于 lhs 中的对应元素,或不存在不相等元素时。对于两个空元组,返回 false
4) ! ( rhs < lhs )
5) rhs < lhs
6) ! ( lhs < rhs )
7,9) 若存在非等价元素对,则返回第一对非等价元素间的关系,否则返回 std::strong_ordering::equal 。对于两个空元组,返回 std::strong_ordering::equal

注释

关系运算符是通过每个元素的 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 中的值
(函数模板)