https://gcc.gnu.org/g:c0d805192b0092235b2ef32a184b17a76ebac401

commit r12-11044-gc0d805192b0092235b2ef32a184b17a76ebac401
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Mon Dec 9 17:35:24 2024 +0000

    libstdc++: Skip redundant assertions in std::array equality [PR106212]
    
    As PR c++/106212 shows, the Debug Mode checks cause a compilation error
    for equality comparisons involving std::array prvalues in constant
    expressions. Those Debug Mode checks are redundant when
    comparing two std::array objects, because we already know we have a
    valid range. We can also avoid the unnecessary step of using
    std::__niter_base to do __normal_iterator unwrapping, which isn't needed
    because our std::array iterators are just pointers. Using
    std::__equal_aux1 instead of std::equal avoids the redundant checks in
    std::equal and std::__equal_aux.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/106212
            * include/std/array (operator==): Use std::__equal_aux1 instead
            of std::equal.
            * testsuite/23_containers/array/comparison_operators/106212.cc:
            New test.
    
    (cherry picked from commit 3aeb2edee2f9fc39ab77c7e020f09d7204b167ac)

Diff:
---
 libstdc++-v3/include/std/array                            |  2 +-
 .../23_containers/array/comparison_operators/106212.cc    | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array
index d8337f482ffe..7cab84688eaa 100644
--- a/libstdc++-v3/include/std/array
+++ b/libstdc++-v3/include/std/array
@@ -302,7 +302,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     _GLIBCXX20_CONSTEXPR
     inline bool
     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
-    { return std::equal(__one.begin(), __one.end(), __two.begin()); }
+    { return std::__equal_aux1(__one.begin(), __one.end(), __two.begin()); }
 
 #if __cpp_lib_three_way_comparison && __cpp_lib_concepts
   template<typename _Tp, size_t _Nm>
diff --git 
a/libstdc++-v3/testsuite/23_containers/array/comparison_operators/106212.cc 
b/libstdc++-v3/testsuite/23_containers/array/comparison_operators/106212.cc
new file mode 100644
index 000000000000..f7b12bd04efd
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/array/comparison_operators/106212.cc
@@ -0,0 +1,15 @@
+// { dg-options "-D_GLIBCXX_DEBUG" }
+// { dg-do compile { target c++20 } }
+
+// Bug libstdc++/106212 - Code becomes non-constexpr with _GLIBCXX_DEBUG
+
+#include <array>
+
+struct A
+{
+  constexpr A(int i) : e{i} {}
+  constexpr bool operator==(const A& a) const = default;
+  std::array<int, 1> e;
+};
+
+static_assert(A{1} != A{2}, "");

Reply via email to