]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Ensure that std::vector<bool> allocator has bool value_type
authorJonathan Wakely <jwakely@redhat.com>
Fri, 21 Mar 2025 22:49:44 +0000 (22:49 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Mon, 24 Mar 2025 11:34:49 +0000 (11:34 +0000)
This is the subject of LWG 4228 which notes that libstdc++ doesn't
enforce this requirement. That's just a bug because I forgot to add it
to vector<bool> when adding it elsewhere.

For consistency with the other containers we should not allow incorrect
allocator types for strict -std=c++NN modes, but it is very late to make
that change for GCC 15 so this only enables the assertion for C++20
(where it's required). For GCC 16 we can enable it for strict modes too.

libstdc++-v3/ChangeLog:

* include/bits/stl_bvector.h (vector<bool, A>): Enforce the
C++20 requirement that the allocator's value_type matches the
container.
* testsuite/23_containers/vector/bool/cons/from_range.cc: Fix
incorrect allocator type.

Reviewed-by: Tomasz KamiƄski <tkaminsk@redhat.com>
libstdc++-v3/include/bits/stl_bvector.h
libstdc++-v3/testsuite/23_containers/vector/bool/cons/from_range.cc

index 2292eec54ad748c33b28a30a164a4214933c6f4b..3ee15eaa938cee07a392f9dbaee47c9530e40855 100644 (file)
@@ -751,6 +751,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 
 #if __cplusplus >= 201103L
       friend struct std::hash<vector>;
+# if __cplusplus > 201703L // || defined __STRICT_ANSI__
+      static_assert(is_same<typename _Alloc::value_type, bool>::value,
+         "std::vector must have the same value_type as its allocator");
+# endif
 #endif
 
     public:
index f531e7f503961fa03e90982f31f5d9ef2b9732db..37f0ecf32ad77cb2194d7ad1ee02f7d04f8432da 100644 (file)
@@ -71,7 +71,7 @@ test_ranges()
     bool val;
   };
   using rvalue_input_range = test_range<C, input_iterator_wrapper_rval>;
-  do_test<rvalue_input_range>(std::allocator<int>());
+  do_test<rvalue_input_range>(std::allocator<bool>());
 
   return true;
 }