[gcc r10-10305] libstdc++: fix is_default_constructible for hash containers [PR 100863]

Jonathan Wakely redi@gcc.gnu.org
Fri Nov 26 16:34:45 GMT 2021


https://gcc.gnu.org/g:5c86e63cb0383a38ec3dea24e9b3fe2f6e312057

commit r10-10305-g5c86e63cb0383a38ec3dea24e9b3fe2f6e312057
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jul 20 15:20:41 2021 +0100

    libstdc++: fix is_default_constructible for hash containers [PR 100863]
    
    The recent change to _Hashtable_ebo_helper for this PR broke the
    is_default_constructible trait for a hash container with a non-default
    constructible allocator. That happens because the constructor needs to
    be user-provided in order to initialize the member, and so is not
    defined as deleted when the type is not default constructible.
    
    By making _Hashtable derive from _Enable_special_members we can ensure
    that the default constructor for the std::unordered_xxx containers is
    deleted when it would be ill-formed. This makes the trait give the
    correct answer.
    
    This backport to gcc-10 includes the fix for PR 101583.
    
    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/100863
            * include/bits/hashtable.h (_Hashtable): Conditionally delete
            default constructor by deriving from _Enable_default_constructor.
            Construct that base-class explicitly in all non-forwarding,
            non-defaulted constructors.
            * testsuite/23_containers/unordered_map/cons/default.cc: New test.
            * testsuite/23_containers/unordered_set/cons/default.cc: New test.
    
    (cherry picked from commit 89ec3b67dbe856a447d068b053bc19559f136f43)

Diff:
---
 libstdc++-v3/include/bits/hashtable.h              | 26 ++++++++++--
 .../23_containers/unordered_map/cons/default.cc    | 48 ++++++++++++++++++++++
 .../23_containers/unordered_set/cons/default.cc    | 47 +++++++++++++++++++++
 3 files changed, 118 insertions(+), 3 deletions(-)

diff --git a/libstdc++-v3/include/bits/hashtable.h b/libstdc++-v3/include/bits/hashtable.h
index 310eb52dd51..961e83049a5 100644
--- a/libstdc++-v3/include/bits/hashtable.h
+++ b/libstdc++-v3/include/bits/hashtable.h
@@ -33,6 +33,7 @@
 #pragma GCC system_header
 
 #include <bits/hashtable_policy.h>
+#include <bits/enable_special_members.h>
 #if __cplusplus > 201402L
 # include <bits/node_handle.h>
 #endif
@@ -48,6 +49,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 		       // Mandatory to have erase not throwing.
 		       __is_nothrow_invocable<const _Hash&, const _Tp&>>>;
 
+  // Helper to conditionally delete the default constructor.
+  // The _Hash_node_base type is used to distinguish this specialization
+  // from any other potentially-overlapping subobjects of the hashtable.
+  template<typename _Equal, typename _Hash, typename _Allocator>
+    using _Hashtable_enable_default_ctor
+      = _Enable_default_constructor<__and_<is_default_constructible<_Equal>,
+				       is_default_constructible<_Hash>,
+				       is_default_constructible<_Allocator>>{},
+				    __detail::_Hash_node_base>;
+
   /**
    *  Primary class template _Hashtable.
    *
@@ -184,7 +195,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       private __detail::_Hashtable_alloc<
 	__alloc_rebind<_Alloc,
 		       __detail::_Hash_node<_Value,
-					    _Traits::__hash_cached::value>>>
+					    _Traits::__hash_cached::value>>>,
+      private _Hashtable_enable_default_ctor<_Equal, _H1, _Alloc>
     {
       static_assert(is_same<typename remove_cv<_Value>::type, _Value>::value,
 	  "unordered container must have a non-const, non-volatile value_type");
@@ -206,6 +218,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	typename __hashtable_alloc::__node_alloc_traits;
       using __node_base = typename __hashtable_alloc::__node_base;
       using __bucket_type = typename __hashtable_alloc::__bucket_type;
+      using __enable_default_ctor
+	= _Hashtable_enable_default_ctor<_Equal, _H1, _Alloc>;
 
     public:
       typedef _Key						key_type;
@@ -443,7 +457,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 		 const _Equal& __eq, const _ExtractKey& __exk,
 		 const allocator_type& __a)
       : __hashtable_base(__exk, __h1, __h2, __h, __eq),
-	__hashtable_alloc(__node_alloc_type(__a))
+	__hashtable_alloc(__node_alloc_type(__a)),
+	__enable_default_ctor(_Enable_default_constructor_tag{})
       { }
 
       template<bool _No_realloc = true>
@@ -504,7 +519,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       // Use delegating constructors.
       explicit
       _Hashtable(const allocator_type& __a)
-      : __hashtable_alloc(__node_alloc_type(__a))
+      : __hashtable_alloc(__node_alloc_type(__a)),
+	__enable_default_ctor(_Enable_default_constructor_tag{})
       { }
 
       explicit
@@ -1302,6 +1318,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       __rehash_base(__ht),
       __hashtable_alloc(
 	__node_alloc_traits::_S_select_on_copy(__ht._M_node_allocator())),
+      __enable_default_ctor(__ht),
       _M_buckets(nullptr),
       _M_bucket_count(__ht._M_bucket_count),
       _M_element_count(__ht._M_element_count),
@@ -1324,6 +1341,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       __map_base(__ht),
       __rehash_base(__ht),
       __hashtable_alloc(std::move(__a)),
+      __enable_default_ctor(__ht),
       _M_buckets(__ht._M_buckets),
       _M_bucket_count(__ht._M_bucket_count),
       _M_before_begin(__ht._M_before_begin._M_nxt),
@@ -1356,6 +1374,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       __map_base(__ht),
       __rehash_base(__ht),
       __hashtable_alloc(__node_alloc_type(__a)),
+      __enable_default_ctor(__ht),
       _M_buckets(),
       _M_bucket_count(__ht._M_bucket_count),
       _M_element_count(__ht._M_element_count),
@@ -1377,6 +1396,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       __map_base(__ht),
       __rehash_base(__ht),
       __hashtable_alloc(std::move(__a)),
+      __enable_default_ctor(__ht),
       _M_buckets(nullptr),
       _M_bucket_count(__ht._M_bucket_count),
       _M_element_count(__ht._M_element_count),
diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/cons/default.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/default.cc
new file mode 100644
index 00000000000..d64d078a7da
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/default.cc
@@ -0,0 +1,48 @@
+// { dg-do compile { target c++11 } }
+#include <unordered_map>
+
+static_assert( std::is_default_constructible<std::unordered_map<int, int>>{}, "" );
+
+template<typename T>
+  struct NoDefaultConsAlloc
+  {
+    using value_type = T;
+
+    NoDefaultConsAlloc(int) noexcept { }
+
+    template<typename U>
+      NoDefaultConsAlloc(const NoDefaultConsAlloc<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); }
+  };
+
+using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>,
+			       NoDefaultConsAlloc<std::pair<const int, int>>>;
+static_assert( ! std::is_default_constructible<Map>{}, "PR libstdc++/100863" );
+
+struct Hash : std::hash<int> { Hash(int) { } };
+using Map2 = std::unordered_map<int, int, Hash>;
+static_assert( ! std::is_default_constructible<Map2>{}, "PR libstdc++/100863" );
+
+struct Equal : std::equal_to<int> { Equal(int) { } };
+using Map3 = std::unordered_map<int, int, std::hash<int>, Equal>;
+static_assert( ! std::is_default_constructible<Map3>{}, "PR libstdc++/100863" );
+
+// PR libstdc++/101583
+// verify non-default ctors can still be used
+using Map4 = std::unordered_map<int, int, Hash, Equal,
+			        NoDefaultConsAlloc<std::pair<const int, int>>>;
+Hash h(1);
+Equal eq(1);
+Map4::allocator_type a(1);
+Map4 m{1, h, eq, a};
+Map4 m2{m.begin(), m.end(), m.size(), h, eq, a};
+Map4 m3{{{1,1}, {2,2}, {3,3}}, 3, h, eq, a};
+Map4 m4{m};
+Map4 m5{m, a};
+Map4 m6{std::move(m)};
+Map4 m7{std::move(m6), a};
diff --git a/libstdc++-v3/testsuite/23_containers/unordered_set/cons/default.cc b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/default.cc
new file mode 100644
index 00000000000..41281d3d774
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/default.cc
@@ -0,0 +1,47 @@
+// { dg-do compile { target c++11 } }
+#include <unordered_set>
+
+static_assert( std::is_default_constructible<std::unordered_set<int>>{}, "" );
+
+template<typename T>
+  struct NoDefaultConsAlloc
+  {
+    using value_type = T;
+
+    NoDefaultConsAlloc(int) noexcept { }
+
+    template<typename U>
+      NoDefaultConsAlloc(const NoDefaultConsAlloc<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); }
+  };
+
+using Set = std::unordered_set<int, std::hash<int>, std::equal_to<int>,
+			       NoDefaultConsAlloc<int>>;
+static_assert( ! std::is_default_constructible<Set>{}, "PR libstdc++/100863" );
+
+struct Hash : std::hash<int> { Hash(int) { } };
+using Set2 = std::unordered_set<int, Hash>;
+static_assert( ! std::is_default_constructible<Set2>{}, "PR libstdc++/100863" );
+
+struct Equal : std::equal_to<int> { Equal(int) { } };
+using Set3 = std::unordered_set<int, std::hash<int>, Equal>;
+static_assert( ! std::is_default_constructible<Set3>{}, "PR libstdc++/100863" );
+
+// PR libstdc++/101583
+// verify non-default ctors can still be used
+using Set4 = std::unordered_set<int, Hash, Equal, NoDefaultConsAlloc<int>>;
+Hash h(1);
+Equal eq(1);
+Set4::allocator_type a(1);
+Set4 s{1, h, eq, a};
+Set4 s2{s.begin(), s.end(), s.size(), h, eq, a};
+Set4 s3{{1, 2, 3}, 3, h, eq, a};
+Set4 s4{s};
+Set4 s5{s, a};
+Set4 s6{std::move(s)};
+Set4 s7{std::move(s6), a};


More information about the Libstdc++-cvs mailing list