|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
(C++11) | ||||
| 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) | ||||
(C++11) | ||||
(C++11) | ||||
| Helper classes | ||||
(C++11) | ||||
(C++11) | ||||
(C++23) | ||||
(C++23) | ||||
(C++11) | ||||
| Deduction guides(C++17) |
Defined in header <utility> | ||
| (1) | ||
| (until C++14) | ||
| (since C++14) | ||
| (2) | ||
| (until C++14) | ||
| (since C++14) (until C++20) | ||
| (3) | ||
| (until C++14) | ||
| (since C++14) (until C++20) | ||
| (4) | ||
| (until C++14) | ||
| (since C++14) (until C++20) | ||
| (5) | ||
| (until C++14) | ||
| (since C++14) (until C++20) | ||
| (6) | ||
| (until C++14) | ||
| (since C++14) (until C++20) | ||
template<class T1,class T2,class U1,class U2> constexprstd::common_comparison_category_t<synth-three-way-result<T1, U1>, | (7) | (since C++20) |
The behavior is undefined if the type and value category of eitherlhs.first== rhs.first orlhs.second== rhs.second do not meet theBooleanTestable requirements. | (until C++26) |
This overload participates in overload resolution only if bothdecltype(lhs.first== rhs.first) anddecltype(lhs.second== rhs.second) model | (since C++26) |
synth-three-way.The | (since C++20) |
Contents |
| lhs, rhs | - | pairs to compare |
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::pair |
Becauseoperator< is defined for pairs, containers of pairs can be sorted.
#include <algorithm>#include <iomanip>#include <iostream>#include <string>#include <utility>#include <vector> int main(){std::vector<std::pair<int,std::string>> v={{2,"baz"},{2,"bar"},{1,"foo"}};std::sort(v.begin(), v.end()); for(auto p: v)std::cout<<'{'<< p.first<<", "<<std::quoted(p.second)<<"}\n";}
Output:
{1, "foo"}{2, "bar"}{2, "baz"}The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 296 | C++98 | the descriptions of operators other than== and< were missing | added |
| LWG 2114 (P2167R3) | C++98 | type preconditions for boolean operations were missing | added |
| LWG 3865 | C++98 | comparison operators only acceptedpairs of the same type | acceptpairs of different types |
(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 tuple (function template)[edit] |