From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id EEF1B384477A; Mon, 15 Apr 2024 18:29:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EEF1B384477A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1713205785; bh=o8ad0avVjp6b2iXImitVHLJkLEyoLzr4MxIjJ2PzU20=; h=From:To:Subject:Date:From; b=qpMkhu8Iy2EF7roI9pRnvW+4wpuqgFUriE/lvYlZZoYO+2ro3/DhnCEgkd0PwNMSA c14/iOwQR8RBY5FkwzFQzpHJmypblbseLWe/3sV/kPqy7ahRX8M0m1V0JnSFUCGMbb C6q9vAIYSuv540wLWsg18GVMnIb0yxSaPoMOhywE= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-9979] libstdc++: Heterogeneous std::pair comparisons [PR113386] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 2d694414ada8e3b58f504c1b175d31088529632e X-Git-Newrev: 2a0c083558b4ac6609692294df7a388cf4468711 Message-Id: <20240415182945.EEF1B384477A@sourceware.org> Date: Mon, 15 Apr 2024 18:29:45 +0000 (GMT) List-Id: https://gcc.gnu.org/g:2a0c083558b4ac6609692294df7a388cf4468711 commit r14-9979-g2a0c083558b4ac6609692294df7a388cf4468711 Author: Jonathan Wakely Date: Mon Jan 15 14:47:52 2024 +0000 libstdc++: Heterogeneous std::pair comparisons [PR113386] I'm only treating this as a DR for C++20 for now, because it's less work and only requires changes to operator== and operator<=>. To do this for older standards would require changes to the six relational operators used pre-C++20. libstdc++-v3/ChangeLog: PR libstdc++/113386 * include/bits/stl_pair.h (operator==, operator<=>): Support heterogeneous comparisons, as per LWG 3865. * testsuite/20_util/pair/comparison_operators/lwg3865.cc: New test. Diff: --- libstdc++-v3/include/bits/stl_pair.h | 32 ++++++++++++++++------ .../20_util/pair/comparison_operators/lwg3865.cc | 15 ++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_pair.h b/libstdc++-v3/include/bits/stl_pair.h index 4f5c8389fa6..45317417c9c 100644 --- a/libstdc++-v3/include/bits/stl_pair.h +++ b/libstdc++-v3/include/bits/stl_pair.h @@ -1000,23 +1000,39 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template pair(_T1, _T2) -> pair<_T1, _T2>; #endif - /// Two pairs of the same type are equal iff their members are equal. - template +#if __cpp_lib_three_way_comparison && __cpp_lib_concepts + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3865. Sorting a range of pairs + + /// Two pairs are equal iff their members are equal. + template inline _GLIBCXX_CONSTEXPR bool - operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) + operator==(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y) { return __x.first == __y.first && __x.second == __y.second; } -#if __cpp_lib_three_way_comparison && __cpp_lib_concepts - template - constexpr common_comparison_category_t<__detail::__synth3way_t<_T1>, - __detail::__synth3way_t<_T2>> - operator<=>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) + /** Defines a lexicographical order for pairs. + * + * For two pairs of comparable types, `P` is ordered before `Q` if + * `P.first` is less than `Q.first`, or if `P.first` and `Q.first` + * are equivalent (neither is less than the other) and `P.second` is + * less than `Q.second`. + */ + template + constexpr common_comparison_category_t<__detail::__synth3way_t<_T1, _U1>, + __detail::__synth3way_t<_T2, _U2>> + operator<=>(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y) { if (auto __c = __detail::__synth3way(__x.first, __y.first); __c != 0) return __c; return __detail::__synth3way(__x.second, __y.second); } #else + /// Two pairs of the same type are equal iff their members are equal. + template + inline _GLIBCXX_CONSTEXPR bool + operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) + { return __x.first == __y.first && __x.second == __y.second; } + /** Defines a lexicographical order for pairs. * * For two pairs of the same type, `P` is ordered before `Q` if diff --git a/libstdc++-v3/testsuite/20_util/pair/comparison_operators/lwg3865.cc b/libstdc++-v3/testsuite/20_util/pair/comparison_operators/lwg3865.cc new file mode 100644 index 00000000000..2bbd54af192 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/pair/comparison_operators/lwg3865.cc @@ -0,0 +1,15 @@ +// { dg-do run { target c++20 } } + +// LWG 3865. Sorting a range of pairs + +#include +#include + +int main() +{ + std::pair p(1, 2); + std::pair p2(p.first, p.second); + VERIFY( p == p2 ); + VERIFY( p <= p2 ); + VERIFY( p >= p2 ); +}