operator==,!=,<,<=,>,>=,<=>(std::tuple)
Defined in header <tuple>
|
||
(1) | ||
template< class... TTypes, class... UTypes > bool operator==( const std::tuple<TTypes...>& lhs, |
(since C++11) (until C++14) |
|
template< class... TTypes, class... UTypes > constexpr bool operator==( const std::tuple<TTypes...>& lhs, |
(since C++14) | |
(2) | ||
template< class... TTypes, class... UTypes > bool operator!=( const std::tuple<TTypes...>& lhs, |
(since C++11) (until C++14) |
|
template< class... TTypes, class... UTypes > constexpr bool operator!=( const std::tuple<TTypes...>& lhs, |
(since C++14) (until C++20) |
|
(3) | ||
template< class... TTypes, class... UTypes > bool operator<( const std::tuple<TTypes...>& lhs, |
(since C++11) (until C++14) |
|
template< class... TTypes, class... UTypes > constexpr bool operator<( const std::tuple<TTypes...>& lhs, |
(since C++14) (until C++20) |
|
(4) | ||
template< class... TTypes, class... UTypes > bool operator<=( const std::tuple<TTypes...>& lhs, |
(since C++11) (until C++14) |
|
template< class... TTypes, class... UTypes > constexpr bool operator<=( const std::tuple<TTypes...>& lhs, |
(since C++14) (until C++20) |
|
(5) | ||
template< class... TTypes, class... UTypes > bool operator>( const std::tuple<TTypes...>& lhs, |
(since C++11) (until C++14) |
|
template< class... TTypes, class... UTypes > constexpr bool operator>( const std::tuple<TTypes...>& lhs, |
(since C++14) (until C++20) |
|
(6) | ||
template< class... TTypes, class... UTypes > bool operator>=( const std::tuple<TTypes...>& lhs, |
(since C++11) (until C++14) |
|
template< class... TTypes, class... UTypes > constexpr bool operator>=( const std::tuple<TTypes...>& lhs, |
(since C++14) (until C++20) |
|
template< class... TTypes, class... UTypes > constexpr /* see below */ operator<=>( const std::tuple<TTypes...>& lhs, |
(7) | (since C++20) |
lhs
with the corresponding element of the tuple rhs
.lhs
and rhs
lexicographically by operator<, that is, compares the first elements, if they are equivalent, compares the second elements, if those are equivalent, compares the third elements, and so on.
For non-empty tuples, (3) is equivalent to
lhs
and rhs
lexicographically by synthesized three-way comparison (see below), that is, compares the first elements, if they are equivalent, compares the second elements, if those are equivalent, compares the third elements, and so on.
The return type is the common comparison category type of results of synthesized three-way comparison on every pair of element in lhs
and rhs
. For empty tuples, the return type is std::strong_ordering.
For non-empty tuples, (7) is equivalent to
synth_three_way
is an exposition-only function object performing synthesized three-way comparison.sizeof...(TTypes) and sizeof...(UTypes) must be equal, otherwise the program is ill-formed or for operator<=>, the operator function does not participate in overload resolution (since C++20). N
in above code is equal to both.
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) |
Synthesized three-way comparisonGiven two object types
t < u ? std::weak_ordering::less : u < t ? std::weak_ordering::greater : std::weak_ordering::equivalent
The behavior of operator<=> is undefined if |
(since C++20) |
Parameters
lhs, rhs | - | tuples to compare |
Return value
[0, sizeof...(Types))
, otherwise false. For two empty tuples returns true.lhs
is less than the one in rhs
, false if the first non-equivalent element in rhs
is less than the one in lhs
or there is no non-equivalent element. For two empty tuples, returns false.Example
Because operator< is defined for tuples, containers of tuples can be sorted.
#include <iostream> #include <tuple> #include <vector> #include <algorithm> 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 << "{" << std::get<0>(p) << ", " << std::get<1>(p) << ", " << std::get<2>(p) << "}\n"; } }
Output:
{1, foo, 10.1} {2, bar, 3.14} {2, baz, -1.1} {2, baz, -0.1}
See also
(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 the pair (function template) |