]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add missing constraints to vector and deque deduction guides
authorJonathan Wakely <jwakely@redhat.com>
Thu, 21 May 2026 17:58:14 +0000 (18:58 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Tue, 26 May 2026 10:13:59 +0000 (11:13 +0100)
The standard requires that these deduction guides are constrained to
only accept a type that qualifies as an allocator for the second
templates argument.

libstdc++-v3/ChangeLog:

* include/bits/stl_deque.h: Add missing constraint on allocator
type in deduction guide.
* include/bits/stl_vector.h: Likewise.
* include/debug/deque: Likewise.
* include/debug/vector: Likewise.
* testsuite/23_containers/deque/cons/deduction_c++23.cc: Check
that deduction fails for a type which does not qualify as an
allocator.
* testsuite/23_containers/vector/cons/deduction_c++23.cc:
Likewise.

Reviewed-by: Tomasz KamiƄski <tkaminsk@redhat.com>
libstdc++-v3/include/bits/stl_deque.h
libstdc++-v3/include/bits/stl_vector.h
libstdc++-v3/include/debug/deque
libstdc++-v3/include/debug/vector
libstdc++-v3/testsuite/23_containers/deque/cons/deduction_c++23.cc [new file with mode: 0644]
libstdc++-v3/testsuite/23_containers/vector/cons/deduction_c++23.cc [new file with mode: 0644]

index aebe865ffc1adc589e06ba529260499a0aa1953d..28f61d2ccd371c359db9e6a84ccfb5842e8ca3dc 100644 (file)
@@ -2414,7 +2414,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 
 #if __glibcxx_containers_ranges // C++ >= 23
   template<ranges::input_range _Rg,
-          typename _Alloc = allocator<ranges::range_value_t<_Rg>>>
+          __allocator_like _Alloc = allocator<ranges::range_value_t<_Rg>>>
     deque(from_range_t, _Rg&&, _Alloc = _Alloc())
       -> deque<ranges::range_value_t<_Rg>, _Alloc>;
 #endif
index 0c9b74fdbb24a7b75b6ff96c68f86f10b7184fd6..9ddf35502e69e856e4574e686314753bb8826839 100644 (file)
@@ -2383,7 +2383,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 
 #if __glibcxx_containers_ranges // C++ >= 23
   template<ranges::input_range _Rg,
-          typename _Alloc = allocator<ranges::range_value_t<_Rg>>>
+          __allocator_like _Alloc = allocator<ranges::range_value_t<_Rg>>>
     vector(from_range_t, _Rg&&, _Alloc = _Alloc())
       -> vector<ranges::range_value_t<_Rg>, _Alloc>;
 #endif
index 9fba4ffc4997804495fa24c02c92e9d93fb486c5..00b4220051e78d3584285ce6f6ca6165e0abd738 100644 (file)
@@ -715,7 +715,7 @@ namespace __debug
 
 #if __glibcxx_containers_ranges // C++ >= 23
   template<ranges::input_range _Rg,
-          typename _Alloc = allocator<ranges::range_value_t<_Rg>>>
+          __allocator_like _Alloc = allocator<ranges::range_value_t<_Rg>>>
     deque(from_range_t, _Rg&&, _Alloc = _Alloc())
       -> deque<ranges::range_value_t<_Rg>, _Alloc>;
 #endif
index 61e5ff78a7a4b7cb211941e64931279d4a57d0c7..56645d1e92c316db949a0766609a6575fc01797b 100644 (file)
@@ -1001,7 +1001,7 @@ namespace __debug
 
 #if __glibcxx_containers_ranges // C++ >= 23
   template<ranges::input_range _Rg,
-          typename _Alloc = allocator<ranges::range_value_t<_Rg>>>
+          __allocator_like _Alloc = allocator<ranges::range_value_t<_Rg>>>
     vector(from_range_t, _Rg&&, _Alloc = _Alloc())
       -> vector<ranges::range_value_t<_Rg>, _Alloc>;
 #endif
diff --git a/libstdc++-v3/testsuite/23_containers/deque/cons/deduction_c++23.cc b/libstdc++-v3/testsuite/23_containers/deque/cons/deduction_c++23.cc
new file mode 100644 (file)
index 0000000..5893de9
--- /dev/null
@@ -0,0 +1,14 @@
+// { dg-do compile { target c++23 } }
+
+#include <deque>
+#include <testsuite_iterators.h>
+
+using Range = __gnu_test::test_input_range<int>;
+
+template<typename Alloc>
+concept can_deduce_deque = requires(Range r, Alloc a) {
+  std::deque(std::from_range, r, a);
+};
+
+// Deduction should fail because int does not qualify as an allocator.
+static_assert( ! can_deduce_deque<int> );
diff --git a/libstdc++-v3/testsuite/23_containers/vector/cons/deduction_c++23.cc b/libstdc++-v3/testsuite/23_containers/vector/cons/deduction_c++23.cc
new file mode 100644 (file)
index 0000000..1a442a8
--- /dev/null
@@ -0,0 +1,14 @@
+// { dg-do compile { target c++23 } }
+
+#include <vector>
+#include <testsuite_iterators.h>
+
+using Range = __gnu_test::test_input_range<int>;
+
+template<typename Alloc>
+concept can_deduce_vector = requires(Range r, Alloc a) {
+  std::vector(std::from_range, r, a);
+};
+
+// Deduction should fail because int does not qualify as an allocator.
+static_assert( ! can_deduce_vector<int> );