From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 5CDFD385803D; Thu, 14 Oct 2021 08:25:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5CDFD385803D MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-9153] libstdc++: Fix non-default constructors for hash containers [PR101583] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: b4c0226e7b9d42cebb287939386e623600318b18 X-Git-Newrev: 3bf56cdf5ec5e07ea34e6be0110ab8fc76641d87 Message-Id: <20211014082524.5CDFD385803D@sourceware.org> Date: Thu, 14 Oct 2021 08:25:24 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 Oct 2021 08:25:24 -0000 https://gcc.gnu.org/g:3bf56cdf5ec5e07ea34e6be0110ab8fc76641d87 commit r11-9153-g3bf56cdf5ec5e07ea34e6be0110ab8fc76641d87 Author: Jonathan Wakely Date: Thu Jul 22 18:49:57 2021 +0100 libstdc++: Fix non-default constructors for hash containers [PR101583] When I added the new mixin to _Hashtable, I forgot to explicitly construct it in each non-default constructor. That means you can't use any constructors unless all three of the hash function, equality function, and allocator are all default constructible. libstdc++-v3/ChangeLog: PR libstdc++/101583 * include/bits/hashtable.h (_Hashtable): Replace mixin with _Enable_default_ctor. Construct it explicitly in all non-forwarding, non-defaulted constructors. * testsuite/23_containers/unordered_map/cons/default.cc: Check non-default constructors can be used. * testsuite/23_containers/unordered_set/cons/default.cc: Likewise. (cherry picked from commit 8ed6cfbbee74ec9e03f2558b9c36f61dd7d4dcfd) Diff: --- libstdc++-v3/include/bits/hashtable.h | 17 ++++++++++++----- .../23_containers/unordered_map/cons/default.cc | 15 +++++++++++++++ .../23_containers/unordered_set/cons/default.cc | 14 ++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/libstdc++-v3/include/bits/hashtable.h b/libstdc++-v3/include/bits/hashtable.h index 9bc50afbb75..d188d9abdd8 100644 --- a/libstdc++-v3/include/bits/hashtable.h +++ b/libstdc++-v3/include/bits/hashtable.h @@ -54,11 +54,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // from any other potentially-overlapping subobjects of the hashtable. template using _Hashtable_enable_default_ctor - = _Enable_special_members<__and_, + = _Enable_default_constructor<__and_, is_default_constructible<_Hash>, is_default_constructible<_Allocator>>{}, - true, true, true, true, true, - __detail::_Hash_node_base>; + __detail::_Hash_node_base>; /** * Primary class template _Hashtable. @@ -228,6 +227,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>; + using __enable_default_ctor + = _Hashtable_enable_default_ctor<_Equal, _Hash, _Alloc>; public: typedef _Key key_type; @@ -482,7 +483,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Hashtable(const _Hash& __h, const _Equal& __eq, const allocator_type& __a) : __hashtable_base(__h, __eq), - __hashtable_alloc(__node_alloc_type(__a)) + __hashtable_alloc(__node_alloc_type(__a)), + __enable_default_ctor(_Enable_default_constructor_tag{}) { } template @@ -549,7 +551,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION 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{}) { } template @@ -1407,6 +1410,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), @@ -1429,6 +1433,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), @@ -1459,6 +1464,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), @@ -1480,6 +1486,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 index e4f836fde3e..d64d078a7da 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_map/cons/default.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/default.cc @@ -31,3 +31,18 @@ static_assert( ! std::is_default_constructible{}, "PR libstdc++/100863" ); struct Equal : std::equal_to { Equal(int) { } }; using Map3 = std::unordered_map, Equal>; static_assert( ! std::is_default_constructible{}, "PR libstdc++/100863" ); + +// PR libstdc++/101583 +// verify non-default ctors can still be used +using Map4 = std::unordered_map>>; +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 index 42fbf3d7997..41281d3d774 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_set/cons/default.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/default.cc @@ -31,3 +31,17 @@ static_assert( ! std::is_default_constructible{}, "PR libstdc++/100863" ); struct Equal : std::equal_to { Equal(int) { } }; using Set3 = std::unordered_set, Equal>; static_assert( ! std::is_default_constructible{}, "PR libstdc++/100863" ); + +// PR libstdc++/101583 +// verify non-default ctors can still be used +using Set4 = std::unordered_set>; +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};