]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Reverse arguments in constraint for std::optional's <=> [PR104606]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 27 Mar 2024 21:51:13 +0000 (21:51 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 2 May 2024 10:57:47 +0000 (11:57 +0100)
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.

(cherry picked from commit 7f65d8267fbfd19cf21a3dc71d27e989e75044a3)

libstdc++-v3/include/std/optional
libstdc++-v3/testsuite/20_util/optional/relops/104606.cc [new file with mode: 0644]

index 29f534b986199f4acc5f25c14af53496027c6bda..e4f3fc65340641522f395d5c34e762f88f9aa1dc 100644 (file)
@@ -1434,7 +1434,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 (file)
index 0000000..2b8df24
--- /dev/null
@@ -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;