From 89ec3b67dbe856a447d068b053bc19559f136f43 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Tue, 20 Jul 2021 15:20:41 +0100 Subject: 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. --- libstdc++-v3/include/bits/hashtable.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'libstdc++-v3/include/bits/hashtable.h') 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"); -- cgit v1.2.3