|
|
Member functions | ||||
Non-member functions | ||||
operator==operator!=operator<operator<=operator>operator>=operator<=> (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20) | ||||
Helper concepts | ||||
(C++23) | ||||
Helper classes | ||||
(C++23) | ||||
(C++23) | ||||
Deduction guides(C++17) |
Defined in header <tuple> | ||
template<class...TTypes,class...UTypes> bool operator==(conststd::tuple<TTypes...>& lhs, | (1) | (since C++11) (constexpr since C++14) |
template<class...TTypes,class...UTypes> bool operator!=(conststd::tuple<TTypes...>& lhs, | (2) | (since C++11) (constexpr since C++14) (until C++20) |
template<class...TTypes,class...UTypes> bool operator<(conststd::tuple<TTypes...>& lhs, | (3) | (since C++11) (constexpr since C++14) (until C++20) |
template<class...TTypes,class...UTypes> bool operator<=(conststd::tuple<TTypes...>& lhs, | (4) | (since C++11) (constexpr since C++14) (until C++20) |
template<class...TTypes,class...UTypes> bool operator>(conststd::tuple<TTypes...>& lhs, | (5) | (since C++11) (constexpr since C++14) (until C++20) |
template<class...TTypes,class...UTypes> bool operator>=(conststd::tuple<TTypes...>& lhs, | (6) | (since C++11) (constexpr since C++14) (until C++20) |
template<class...TTypes,class...UTypes> constexprstd::common_comparison_category_t< | (7) | (since C++20) |
template<class...TTypes, tuple-like UTuple> constexprbool operator==(const tuple<TTypes...>& lhs,const UTuple& rhs); | (8) | (since C++23) |
template<class...TTypes, tuple-like UTuple> constexprstd::common_comparison_category_t< | (9) | (since C++23) |
Ifsizeof...(TTypes) does not equalsizeof...(UTypes), orstd::get<i>(lhs)== std::get<i>(rhs) is not a valid expression for anyi in [ 0, sizeof...(Types)) , the program is ill-formed. If the type and value category ofstd::get<i>(lhs)== std::get<i>(rhs) do not meet theBooleanTestable requirements for anyi in [ 0, sizeof...(Types)) , the behavior is undefined. | (until C++26) |
This overload participates in overload resolution only ifsizeof...(TTypes) equalssizeof...(UTypes),std::get<i>(lhs)== std::get<i>(rhs) is a valid expression anddecltype(std::get<i>(lhs)== std::get<i>(rhs)) model boolean-testable for everyi in[ 0, sizeof...(Types)) . | (since C++26) |
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;
...
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
object, and the number of elements ofrhs is determined bystd::tuple_size_v<UTuple> instead. This overload can only be found viaargument-dependent lookup.tuple-like
object./* Elems */ denotes the pack of typesstd::tuple_element_t<i, UTuple> for eachi in[
0,
std::tuple_size_v<UTuple>)
in increasing order. This overload can only be found viaargument-dependent lookup.All comparison operators are short-circuited; they do not access tuple elements beyond what is necessary to determine the result of the comparison.
The | (since C++20) |
Contents |
lhs, rhs | - | tuples to compare |
[
0,
sizeof...(Types))
, otherwisefalse. For two empty tuples returnstrue.The relational operators are defined in terms of each element'soperator<. | (until C++20) |
The relational operators are defined in terms ofsynth-three-way, which usesoperator<=> if possible, oroperator< otherwise. Notably, if an element type does not itself provideoperator<=>, but is implicitly convertible to a three-way comparable type, that conversion will be used instead ofoperator<. | (since C++20) |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_constrained_equality | 202403L | (C++26) | Constrainedoperator== forstd::tuple |
Becauseoperator< is defined for tuples, containers of tuples can be sorted.
#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";}
Output:
{ 1, foo, 10.1 }{ 2, bar, 3.14 }{ 2, baz, -1.1 }{ 2, baz, -0.1 }
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2114 (P2167R3) | C++11 | type preconditions for boolean operations were missing | added |
(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20) | lexicographically compares the values in thepair (function template)[edit] |