From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 1E37E385840D; Tue, 12 Oct 2021 19:41:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1E37E385840D 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-9140] libstdc++: fix is_default_constructible for hash containers [PR 100863] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 573c2ffd3cacde5c54605eb0d8b312d22594f7fa X-Git-Newrev: 10c0df1048fdb6404328d4966a3737d4f784c48f Message-Id: <20211012194133.1E37E385840D@sourceware.org> Date: Tue, 12 Oct 2021 19:41:33 +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: Tue, 12 Oct 2021 19:41:33 -0000 https://gcc.gnu.org/g:10c0df1048fdb6404328d4966a3737d4f784c48f commit r11-9140-g10c0df1048fdb6404328d4966a3737d4f784c48f Author: Jonathan Wakely 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. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: PR libstdc++/100863 * include/bits/hashtable.h (_Hashtable): Conditionally delete default constructor by deriving from _Enable_special_members. * 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 | 15 +++++++++- .../23_containers/unordered_map/cons/default.cc | 33 ++++++++++++++++++++++ .../23_containers/unordered_set/cons/default.cc | 33 ++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/bits/hashtable.h b/libstdc++-v3/include/bits/hashtable.h index ce068d5d93f..9bc50afbb75 100644 --- a/libstdc++-v3/include/bits/hashtable.h +++ b/libstdc++-v3/include/bits/hashtable.h @@ -33,6 +33,7 @@ #pragma GCC system_header #include +#include #if __cplusplus > 201402L # include #endif @@ -48,6 +49,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // Mandatory to have erase not throwing. __is_nothrow_invocable>>; + // 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 + using _Hashtable_enable_default_ctor + = _Enable_special_members<__and_, + is_default_constructible<_Hash>, + is_default_constructible<_Allocator>>{}, + true, true, true, true, true, + __detail::_Hash_node_base>; + /** * Primary class template _Hashtable. * @@ -183,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, _Hash, _Alloc> { static_assert(is_same::type, _Value>::value, "unordered container must have a non-const, non-volatile value_type"); 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..e4f836fde3e --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/default.cc @@ -0,0 +1,33 @@ +// { dg-do compile { target c++11 } } +#include + +static_assert( std::is_default_constructible>{}, "" ); + +template + struct NoDefaultConsAlloc + { + using value_type = T; + + NoDefaultConsAlloc(int) noexcept { } + + template + NoDefaultConsAlloc(const NoDefaultConsAlloc&) { } + + T *allocate(std::size_t n) + { return std::allocator().allocate(n); } + + void deallocate(T *p, std::size_t n) + { std::allocator().deallocate(p, n); } + }; + +using Map = std::unordered_map, std::equal_to, + NoDefaultConsAlloc>>; +static_assert( ! std::is_default_constructible{}, "PR libstdc++/100863" ); + +struct Hash : std::hash { Hash(int) { } }; +using Map2 = std::unordered_map; +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" ); 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..42fbf3d7997 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/default.cc @@ -0,0 +1,33 @@ +// { dg-do compile { target c++11 } } +#include + +static_assert( std::is_default_constructible>{}, "" ); + +template + struct NoDefaultConsAlloc + { + using value_type = T; + + NoDefaultConsAlloc(int) noexcept { } + + template + NoDefaultConsAlloc(const NoDefaultConsAlloc&) { } + + T *allocate(std::size_t n) + { return std::allocator().allocate(n); } + + void deallocate(T *p, std::size_t n) + { std::allocator().deallocate(p, n); } + }; + +using Set = std::unordered_set, std::equal_to, + NoDefaultConsAlloc>; +static_assert( ! std::is_default_constructible{}, "PR libstdc++/100863" ); + +struct Hash : std::hash { Hash(int) { } }; +using Set2 = std::unordered_set; +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" );