https://gcc.gnu.org/g:869accb241c84f132ac0c9cd4e5ad9b4b7e6d536

commit r16-352-g869accb241c84f132ac0c9cd4e5ad9b4b7e6d536
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Thu May 1 22:41:40 2025 +0100

    libstdc++: Make __gnu_test::default_init_allocator usable in constexpr
    
    If we make this test allocator usable in constant expressions then we'll
    get an error if the 'state' data member isn't initialized. This makes it
    a more reliable check that allocators are correctly value-initialized
    when they're required to be.
    
    libstdc++-v3/ChangeLog:
    
            * testsuite/23_containers/vector/allocator/default_init.cc:
            Add a check using constant evaluation.
            * testsuite/23_containers/vector/bool/allocator/default_init.cc:
            Likewise.
            * testsuite/util/testsuite_allocator.h (default_init_allocator):
            Make all member functions and equality ops constexpr.

Diff:
---
 .../23_containers/vector/allocator/default_init.cc      | 11 +++++++++++
 .../23_containers/vector/bool/allocator/default_init.cc | 11 +++++++++++
 libstdc++-v3/testsuite/util/testsuite_allocator.h       | 17 ++++++++++-------
 3 files changed, 32 insertions(+), 7 deletions(-)

diff --git 
a/libstdc++-v3/testsuite/23_containers/vector/allocator/default_init.cc 
b/libstdc++-v3/testsuite/23_containers/vector/allocator/default_init.cc
index 195cd2dca544..486c44c9699d 100644
--- a/libstdc++-v3/testsuite/23_containers/vector/allocator/default_init.cc
+++ b/libstdc++-v3/testsuite/23_containers/vector/allocator/default_init.cc
@@ -59,6 +59,17 @@ void test02()
   tmp->~test_type();
 }
 
+#ifdef __cpp_lib_constexpr_vector
+constexpr bool
+test03()
+{
+  using alloc_type = default_init_allocator<T>;
+  std::vector<T, alloc_type> v;
+  return v.get_allocator().state == 0;
+}
+static_assert( test03() );
+#endif
+
 int main()
 {
   test01();
diff --git 
a/libstdc++-v3/testsuite/23_containers/vector/bool/allocator/default_init.cc 
b/libstdc++-v3/testsuite/23_containers/vector/bool/allocator/default_init.cc
index 3914b7fe6c3a..c95cb6ba99f3 100644
--- a/libstdc++-v3/testsuite/23_containers/vector/bool/allocator/default_init.cc
+++ b/libstdc++-v3/testsuite/23_containers/vector/bool/allocator/default_init.cc
@@ -59,6 +59,17 @@ void test02()
   tmp->~test_type();
 }
 
+#ifdef __cpp_lib_constexpr_vector
+constexpr bool
+test03()
+{
+  using alloc_type = default_init_allocator<T>;
+  std::vector<T, alloc_type> v;
+  return v.get_allocator().state == 0;
+}
+static_assert( test03() );
+#endif
+
 int main()
 {
   test01();
diff --git a/libstdc++-v3/testsuite/util/testsuite_allocator.h 
b/libstdc++-v3/testsuite/util/testsuite_allocator.h
index be596bf00fb4..e5ffad2ba587 100644
--- a/libstdc++-v3/testsuite/util/testsuite_allocator.h
+++ b/libstdc++-v3/testsuite/util/testsuite_allocator.h
@@ -541,15 +541,16 @@ namespace __gnu_test
       default_init_allocator() = default;
 
       template<typename U>
+       constexpr
         default_init_allocator(const default_init_allocator<U>& a)
          : state(a.state)
         { }
 
-      T*
+      constexpr T*
       allocate(std::size_t n)
       { return std::allocator<T>().allocate(n); }
 
-      void
+      constexpr void
       deallocate(T* p, std::size_t n)
       { std::allocator<T>().deallocate(p, n); }
 
@@ -557,15 +558,17 @@ namespace __gnu_test
     };
 
   template<typename T, typename U>
-    bool operator==(const default_init_allocator<T>& t,
-                   const default_init_allocator<U>& u)
+    constexpr bool
+    operator==(const default_init_allocator<T>& t,
+              const default_init_allocator<U>& u)
     { return t.state == u.state; }
 
   template<typename T, typename U>
-    bool operator!=(const default_init_allocator<T>& t,
-                   const default_init_allocator<U>& u)
+    constexpr bool
+    operator!=(const default_init_allocator<T>& t,
+              const default_init_allocator<U>& u)
     { return !(t == u); }
-#endif
+#endif // C++11
 
   template<typename Tp>
     struct ExplicitConsAlloc : std::allocator<Tp>

Reply via email to