diff mbox series

libstdc++: Constrain std::vector default constructor [PR113841]

Message ID 20240319155858.3406992-1-jwakely@redhat.com
State New
Headers show
Series libstdc++: Constrain std::vector default constructor [PR113841] | expand

Commit Message

Jonathan Wakely March 19, 2024, 3:57 p.m. UTC
This fixes the problem in the PR, which is revealed by the new
concept-based constraints on std::pair constructors in C++20 mode. That
makes this a C++20 regression, as the PR's example compiles with C++17.

We need something similar for std::basic_string too, which I'll do
later.

Any comments?

Tested aarch64-linux.

-- >8 --

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.
---
 libstdc++-v3/include/bits/allocator.h         |  3 ++
 libstdc++-v3/include/bits/stl_bvector.h       |  3 ++
 libstdc++-v3/include/bits/stl_vector.h        |  3 ++
 .../23_containers/vector/cons/113841.cc       | 34 +++++++++++++++++++
 4 files changed, 43 insertions(+)
 create mode 100644 libstdc++-v3/testsuite/23_containers/vector/cons/113841.cc

Comments

Jonathan Wakely March 22, 2024, 10:50 p.m. UTC | #1
Pushed to trunk. Backport to gcc-13 needed too, as the changes to use
concepts for std::pair constructors are on that branch.

On Tue, 19 Mar 2024 at 15:59, Jonathan Wakely <jwakely@redhat.com> wrote:
>
> This fixes the problem in the PR, which is revealed by the new
> concept-based constraints on std::pair constructors in C++20 mode. That
> makes this a C++20 regression, as the PR's example compiles with C++17.
>
> We need something similar for std::basic_string too, which I'll do
> later.
>
> Any comments?
>
> Tested aarch64-linux.
>
> -- >8 --
>
> 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.
> ---
>  libstdc++-v3/include/bits/allocator.h         |  3 ++
>  libstdc++-v3/include/bits/stl_bvector.h       |  3 ++
>  libstdc++-v3/include/bits/stl_vector.h        |  3 ++
>  .../23_containers/vector/cons/113841.cc       | 34 +++++++++++++++++++
>  4 files changed, 43 insertions(+)
>  create mode 100644 libstdc++-v3/testsuite/23_containers/vector/cons/113841.cc
>
> diff --git a/libstdc++-v3/include/bits/allocator.h b/libstdc++-v3/include/bits/allocator.h
> index ff4f5b9137b..9e75b37fce7 100644
> --- a/libstdc++-v3/include/bits/allocator.h
> +++ b/libstdc++-v3/include/bits/allocator.h
> @@ -254,6 +254,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>      {
>      public:
>        typedef _Tp value_type;
> +      allocator() { }
>        template<typename _Up> allocator(const allocator<_Up>&) { }
>      };
>
> @@ -262,6 +263,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>      {
>      public:
>        typedef _Tp value_type;
> +      allocator() { }
>        template<typename _Up> allocator(const allocator<_Up>&) { }
>      };
>
> @@ -270,6 +272,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>      {
>      public:
>        typedef _Tp value_type;
> +      allocator() { }
>        template<typename _Up> allocator(const allocator<_Up>&) { }
>      };
>    /// @endcond
> diff --git a/libstdc++-v3/include/bits/stl_bvector.h b/libstdc++-v3/include/bits/stl_bvector.h
> index a3343d95b36..d567e26f4e4 100644
> --- a/libstdc++-v3/include/bits/stl_bvector.h
> +++ b/libstdc++-v3/include/bits/stl_bvector.h
> @@ -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()
>         { }
>
> diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h
> index a8d387f40a1..31169711a48 100644
> --- a/libstdc++-v3/include/bits/stl_vector.h
> +++ b/libstdc++-v3/include/bits/stl_vector.h
> @@ -135,6 +135,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
> index 00000000000..a7721d27f79
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/23_containers/vector/cons/113841.cc
> @@ -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);
> +}
> --
> 2.44.0
>
diff mbox series

Patch

diff --git a/libstdc++-v3/include/bits/allocator.h b/libstdc++-v3/include/bits/allocator.h
index ff4f5b9137b..9e75b37fce7 100644
--- a/libstdc++-v3/include/bits/allocator.h
+++ b/libstdc++-v3/include/bits/allocator.h
@@ -254,6 +254,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
     public:
       typedef _Tp value_type;
+      allocator() { }
       template<typename _Up> allocator(const allocator<_Up>&) { }
     };
 
@@ -262,6 +263,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
     public:
       typedef _Tp value_type;
+      allocator() { }
       template<typename _Up> allocator(const allocator<_Up>&) { }
     };
 
@@ -270,6 +272,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
     public:
       typedef _Tp value_type;
+      allocator() { }
       template<typename _Up> allocator(const allocator<_Up>&) { }
     };
   /// @endcond
diff --git a/libstdc++-v3/include/bits/stl_bvector.h b/libstdc++-v3/include/bits/stl_bvector.h
index a3343d95b36..d567e26f4e4 100644
--- a/libstdc++-v3/include/bits/stl_bvector.h
+++ b/libstdc++-v3/include/bits/stl_bvector.h
@@ -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()
 	{ }
 
diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h
index a8d387f40a1..31169711a48 100644
--- a/libstdc++-v3/include/bits/stl_vector.h
+++ b/libstdc++-v3/include/bits/stl_vector.h
@@ -135,6 +135,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
index 00000000000..a7721d27f79
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/vector/cons/113841.cc
@@ -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);
+}