public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [PATCH 1/4] libstdc++: Heterogeneous std::pair comparisons [PR113386]
Date: Wed, 10 Apr 2024 09:45:53 +0100	[thread overview]
Message-ID: <20240410085039.267589-1-jwakely@redhat.com> (raw)

Tested x86_64-linux.

Since this only affects C++20 and later it seems OK for trunk now.

-- >8 --

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.
---
 libstdc++-v3/include/bits/stl_pair.h          | 32 ++++++++++++++-----
 .../pair/comparison_operators/lwg3865.cc      | 15 +++++++++
 2 files changed, 39 insertions(+), 8 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/20_util/pair/comparison_operators/lwg3865.cc

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<typename _T1, typename _T2> pair(_T1, _T2) -> pair<_T1, _T2>;
 #endif
 
-  /// Two pairs of the same type are equal iff their members are equal.
-  template<typename _T1, typename _T2>
+#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<typename _T1, typename _T2, typename _U1, typename _U2>
     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<typename _T1, typename _T2>
-    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<typename _T1, typename _T2, typename _U1, typename _U2>
+    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<typename _T1, typename _T2>
+    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 <utility>
+#include <testsuite_hooks.h>
+
+int main()
+{
+  std::pair<int, int> p(1, 2);
+  std::pair<int&, int&> p2(p.first, p.second);
+  VERIFY( p == p2 );
+  VERIFY( p <= p2 );
+  VERIFY( p >= p2 );
+}
-- 
2.44.0


             reply	other threads:[~2024-04-10  8:50 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-10  8:45 Jonathan Wakely [this message]
2024-04-10  8:45 ` [PATCH 2/4] libstdc++: Add std::reference_wrapper comparison operators for C++26 Jonathan Wakely
2024-04-15 18:30   ` Jonathan Wakely
2024-04-10  8:45 ` [PATCH 3/4] libstdc++: Constrain equality ops for std::pair, std::tuple, std::variant Jonathan Wakely
2024-04-10  8:45 ` [PATCH 4/4] libstdc++: Simplify std::variant comparison operators Jonathan Wakely
2024-04-10 10:45   ` [PATCH 5/4] libstdc++: Rewrite std::variant comparisons without macros Jonathan Wakely
2024-05-07 13:45   ` [PATCH 4/4] libstdc++: Simplify std::variant comparison operators Jonathan Wakely
2024-04-15 18:29 ` [PATCH 1/4] libstdc++: Heterogeneous std::pair comparisons [PR113386] Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240410085039.267589-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).