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: [committed] libstdc++: Reverse arguments in constraint for std::optional's <=> [PR104606]
Date: Wed,  3 Apr 2024 11:53:23 +0100	[thread overview]
Message-ID: <20240403105343.222775-1-jwakely@redhat.com> (raw)

Tested x86_64-linux. Pushed to trunk. Backports likely to follow.

-- >8 --

This is a workaround for a possible compiler bug that causes constraint
recursion in the operator<=>(const optional<T>&, const U&) overload.

libstdc++-v3/ChangeLog:

	PR libstdc++/104606
	* include/std/optional (operator<=>(const optional<T>&, const U&)):
	Reverse order of three_way_comparable_with template arguments.
	* testsuite/20_util/optional/relops/104606.cc: New test.
---
 libstdc++-v3/include/std/optional              |  2 +-
 .../20_util/optional/relops/104606.cc          | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 libstdc++-v3/testsuite/20_util/optional/relops/104606.cc

diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional
index 1e88e8b8880..3507c36a4d8 100644
--- a/libstdc++-v3/include/std/optional
+++ b/libstdc++-v3/include/std/optional
@@ -1430,7 +1430,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #ifdef __cpp_lib_three_way_comparison
   template<typename _Tp, typename _Up>
     requires (!__is_optional_v<_Up>)
-      && three_way_comparable_with<_Tp, _Up>
+      && three_way_comparable_with<_Up, _Tp>
     constexpr compare_three_way_result_t<_Tp, _Up>
     operator<=>(const optional<_Tp>& __x, const _Up& __v)
     { return bool(__x) ? *__x <=> __v : strong_ordering::less; }
diff --git a/libstdc++-v3/testsuite/20_util/optional/relops/104606.cc b/libstdc++-v3/testsuite/20_util/optional/relops/104606.cc
new file mode 100644
index 00000000000..2b8df245219
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/optional/relops/104606.cc
@@ -0,0 +1,18 @@
+// { dg-do compile { target c++17 } }
+
+// Bug 104606 comparison operator resolution with std::optional and -std=c++20
+
+#include <optional>
+#include <variant>
+#include <vector>
+
+struct Value : std::variant<std::vector<Value>> { };
+
+struct Comparator {
+  template <typename T> bool operator<=(const T &) { return true; }
+};
+
+std::optional<Value> o;
+Comparator c;
+
+auto x = c <= o;
-- 
2.44.0


                 reply	other threads:[~2024-04-03 10:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20240403105343.222775-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).