]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Implement LWG 3683 for pmr::polymorphic_allocator
authorJonathan Wakely <jwakely@redhat.com>
Thu, 19 May 2022 12:26:49 +0000 (13:26 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 19 May 2022 14:30:56 +0000 (15:30 +0100)
This issue has recently been moved to Tentatively Ready, and seems
uncontroversial. This allows equality comparison with types that are
convertible to pmr::polymorphic_allocator, which fail deduction for the
existing equality operator.

libstdc++-v3/ChangeLog:

* include/std/memory_resource (polymorphic_allocator): Add
non-template equality operator, as proposed for LWG 3683.
* testsuite/20_util/polymorphic_allocator/lwg3683.cc: New test.

libstdc++-v3/include/std/memory_resource
libstdc++-v3/testsuite/20_util/polymorphic_allocator/lwg3683.cc [new file with mode: 0644]

index 88e8abd60faddbb2cc7a9a95df0cae6fa47f30ff..745422ad5eba549e5dcca8fab8d74da6767ff995 100644 (file)
@@ -356,6 +356,22 @@ namespace pmr
       __attribute__((__returns_nonnull__))
       { return _M_resource; }
 
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // 3683. operator== for polymorphic_allocator cannot deduce template arg
+      [[nodiscard]]
+      friend bool
+      operator==(const polymorphic_allocator& __a,
+                const polymorphic_allocator& __b) noexcept
+      { return *__a.resource() == *__b.resource(); }
+
+#if __cpp_impl_three_way_comparison < 201907L
+      [[nodiscard]]
+      friend bool
+      operator!=(const polymorphic_allocator& __a,
+                const polymorphic_allocator& __b) noexcept
+      { return !(__a == __b); }
+#endif
+
     private:
 #if ! __cpp_lib_make_obj_using_allocator
       using __uses_alloc1_ = __uses_alloc1<polymorphic_allocator>;
diff --git a/libstdc++-v3/testsuite/20_util/polymorphic_allocator/lwg3683.cc b/libstdc++-v3/testsuite/20_util/polymorphic_allocator/lwg3683.cc
new file mode 100644 (file)
index 0000000..acc91f5
--- /dev/null
@@ -0,0 +1,13 @@
+// { dg-do compile { target c++17 } }
+
+#include <memory_resource>
+
+bool
+test_lwg3683(const std::pmr::polymorphic_allocator<int>& a)
+{
+  if (a == std::pmr::get_default_resource())
+    return true;
+  if (std::pmr::get_default_resource() != a)
+    return false;
+  throw a;
+}