From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 15FEA3864814 for ; Tue, 20 Jul 2021 15:26:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 15FEA3864814 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-581-Y3O-BSjtN7-wvzLP_cYm8g-1; Tue, 20 Jul 2021 11:26:22 -0400 X-MC-Unique: Y3O-BSjtN7-wvzLP_cYm8g-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CC0A81966320; Tue, 20 Jul 2021 15:26:21 +0000 (UTC) Received: from localhost (unknown [10.33.36.7]) by smtp.corp.redhat.com (Postfix) with ESMTP id 72FAB6267C; Tue, 20 Jul 2021 15:26:21 +0000 (UTC) Date: Tue, 20 Jul 2021 16:26:20 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: fix is_default_constructible for hash containers [PR 100863] Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="Ns3c4jfYbwDXBH2P" Content-Disposition: inline X-Spam-Status: No, score=-14.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Jul 2021 15:26:26 -0000 --Ns3c4jfYbwDXBH2P Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On 02/06/21 13:35 +0100, Jonathan Wakely wrote: >The allocator, hash function and equality function should all be >value-initialized by the default constructor of an unordered container. >Do it in the EBO helper, so we don't have to get it right in multiple >places. > >Signed-off-by: Jonathan Wakely > >libstdc++-v3/ChangeLog: > > PR libstdc++/100863 > PR libstdc++/65816 > * include/bits/hashtable_policy.h (_Hashtable_ebo_helper): > Value-initialize subobject. > * testsuite/23_containers/unordered_map/allocator/default_init.cc: > Remove XFAIL. > * testsuite/23_containers/unordered_set/allocator/default_init.cc: > Remove XFAIL. 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. Tested powerpc64le-linux. Committed to trunk. --Ns3c4jfYbwDXBH2P Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 89ec3b67dbe856a447d068b053bc19559f136f43 Author: Jonathan Wakely Date: Tue Jul 20 15:20:41 2021 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. diff --git a/libstdc++-v3/include/bits/hashtable.h b/libstdc++-v3/include/bits/hashtable.h index dfc2a2a7800..adb59213f2d 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" ); --Ns3c4jfYbwDXBH2P--