]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Constrain std::vector default constructor [PR113841]
authorJonathan Wakely <jwakely@redhat.com>
Fri, 9 Feb 2024 17:06:20 +0000 (17:06 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 3 Apr 2024 10:45:29 +0000 (11:45 +0100)
This is needed to avoid errors outside the immediate context when
evaluating is_default_constructible_v<vector<T, A>> when A is not
default constructible.

To avoid diagnostic regressions for 23_containers/vector/48101_neg.cc we
need to make the std::allocator<cv T> partial specializations default
constructible, which they probably should have been anyway.

libstdc++-v3/ChangeLog:

PR libstdc++/113841
* include/bits/allocator.h (allocator<cv T>): Add default
constructor to partial specializations for cv-qualified types.
* include/bits/stl_vector.h (_Vector_impl::_Vector_impl()):
Constrain so that it's only present if the allocator is default
constructible.
* include/bits/stl_bvector.h (_Bvector_impl::_Bvector_impl()):
Likewise.
* testsuite/23_containers/vector/cons/113841.cc: New test.

(cherry picked from commit 142cc4c223d695e515ed2504501b91d8a7ac6eb8)

libstdc++-v3/include/bits/allocator.h
libstdc++-v3/include/bits/stl_bvector.h
libstdc++-v3/include/bits/stl_vector.h
libstdc++-v3/testsuite/23_containers/vector/cons/113841.cc [new file with mode: 0644]

index abbd753d33d6a2b3a6e4d2f3a828e896c1ef6b1a..27e47521fe0fb2157f5adf00e8b4c92c5d8dafb6 100644 (file)
@@ -256,6 +256,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
     public:
       typedef _Tp value_type;
+      allocator() { }
       template<typename _Up> allocator(const allocator<_Up>&) { }
     };
 
@@ -264,6 +265,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
     public:
       typedef _Tp value_type;
+      allocator() { }
       template<typename _Up> allocator(const allocator<_Up>&) { }
     };
 
@@ -272,6 +274,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
     public:
       typedef _Tp value_type;
+      allocator() { }
       template<typename _Up> allocator(const allocator<_Up>&) { }
     };
   /// @endcond
index 8b54292e51b9cde31f6b7aa47928519eee1de260..e18de7c62aa2d2d6e8b9652b6244df9d76ba9989 100644 (file)
@@ -593,6 +593,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        _GLIBCXX20_CONSTEXPR
        _Bvector_impl() _GLIBCXX_NOEXCEPT_IF(
          is_nothrow_default_constructible<_Bit_alloc_type>::value)
+#if __cpp_concepts
+       requires is_default_constructible_v<_Bit_alloc_type>
+#endif
        : _Bit_alloc_type()
        { }
 
index acb29396d2640974e6e959f000895855318a4591..798e66d91ac866d38001a3dc9b051f48a61c1f10 100644 (file)
@@ -136,6 +136,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        _GLIBCXX20_CONSTEXPR
        _Vector_impl() _GLIBCXX_NOEXCEPT_IF(
            is_nothrow_default_constructible<_Tp_alloc_type>::value)
+#if __cpp_lib_concepts
+       requires is_default_constructible_v<_Tp_alloc_type>
+#endif
        : _Tp_alloc_type()
        { }
 
diff --git a/libstdc++-v3/testsuite/23_containers/vector/cons/113841.cc b/libstdc++-v3/testsuite/23_containers/vector/cons/113841.cc
new file mode 100644 (file)
index 0000000..a7721d2
--- /dev/null
@@ -0,0 +1,34 @@
+// { dg-do compile { target c++20 } }
+
+#include <vector>
+
+template<typename T>
+struct Alloc
+{
+  using value_type = T;
+
+  Alloc(int) { } // not default constructible
+
+  template<typename U> Alloc(const Alloc<U>&) { }
+
+  T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
+  void deallocate(T* p, std::size_t n) { std::allocator<T>().deallocate(p, n); }
+};
+
+template<typename T> struct wrap { T t; };
+
+template<typename T> void do_adl(T&) { }
+
+void test_pr113841()
+{
+  using test_type = std::vector<int, Alloc<int>>;
+  std::pair<const int, wrap<test_type>>* h = nullptr;
+  do_adl(h);
+}
+
+void test_pr113841_bool()
+{
+  using test_type = std::vector<bool, Alloc<bool>>;
+  std::pair<const int, wrap<test_type>>* h = nullptr;
+  do_adl(h);
+}