This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Remove redundant std::allocator members for C++20


On 22/10/19 23:09 +0100, Jonathan Wakely wrote:
On 22/10/19 22:40 +0100, Jonathan Wakely wrote:
C++20 removes a number of std::allocator members that have correct
defaults provided by std::allocator_traits, so aren't needed.

Several extensions including __gnu_cxx::hash_map and tr1 containers are
no longer usable with std::allocator in C++20 mode. They need to be
updated to use __gnu_cxx::__alloc_traits in a follow-up patch.

Tested powerpc64le-linux, committed to trunk.

I forgot to add the [[deprecated]] attribute to the members in C++17
mode, I'll do that when I fix the extensions and TR1 containers to
work in C++20 mode again.

This fixes the extensions, but still no deprecated attribute on the
std::allocator members.

Committed to trunk.

commit ea034f3d691dcd45d4ae750b69717d2cf19df6d4
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Oct 23 00:16:25 2019 +0100

    Adjust extension types to use allocator_traits
    
    This makes these extensions work with types meeting the Cpp17Allocator
    requirements as well as the C++98 Allocator requirements.
    
            * include/backward/hash_set (hash_set): Use __alloc_traits.
            * include/backward/hashtable.h (_Hashtable): Likewise.
            * include/ext/alloc_traits.h (__alloc_traits::allocate): Add overload
            taking a hint.
            * include/ext/extptr_allocator.h (_ExtPtr_allocator::allocate): Ignore
            hint.
            * include/ext/slist (_Slist_base): Use __alloc_traits.
            * include/tr1/hashtable.h (_Hashtable): Likewise.
            * include/tr1/regex (match_results): Use vector::const_reference
            instead of assuming the allocator defines it.
            * testsuite/backward/hash_map/23528.cc: Use allocator_traits in C++11.
            * testsuite/tr1/6_containers/unordered_map/capacity/29134-map.cc: Use
            __gnu_test::max_size.
            * testsuite/tr1/6_containers/unordered_multimap/capacity/
            29134-multimap.cc: Likewise.
            * testsuite/tr1/6_containers/unordered_multiset/capacity/
            29134-multiset.cc: Likewise.
            * testsuite/tr1/6_containers/unordered_set/capacity/29134-set.cc:
            Likewise.

diff --git a/libstdc++-v3/include/backward/hash_set b/libstdc++-v3/include/backward/hash_set
index 1445aa61e11..7f743fdf3af 100644
--- a/libstdc++-v3/include/backward/hash_set
+++ b/libstdc++-v3/include/backward/hash_set
@@ -88,6 +88,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       __glibcxx_class_requires3(_HashFcn, size_t, _Value, _UnaryFunctionConcept)
       __glibcxx_class_requires3(_EqualKey, _Value, _Value, _BinaryPredicateConcept)
 
+      typedef __alloc_traits<_Alloc> _Alloc_traits;
+
     private:
       typedef hashtable<_Value, _Value, _HashFcn, _Identity<_Value>,
 			_EqualKey, _Alloc> _Ht;
@@ -101,10 +103,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       
       typedef typename _Ht::size_type size_type;
       typedef typename _Ht::difference_type difference_type;
-      typedef typename _Alloc::pointer pointer;
-      typedef typename _Alloc::const_pointer const_pointer;
-      typedef typename _Alloc::reference reference;
-      typedef typename _Alloc::const_reference const_reference;
+      typedef typename _Alloc_traits::pointer pointer;
+      typedef typename _Alloc_traits::const_pointer const_pointer;
+      typedef typename _Alloc_traits::reference reference;
+      typedef typename _Alloc_traits::const_reference const_reference;
       
       typedef typename _Ht::const_iterator iterator;
       typedef typename _Ht::const_iterator const_iterator;
diff --git a/libstdc++-v3/include/backward/hashtable.h b/libstdc++-v3/include/backward/hashtable.h
index df6ad85191c..cfb9cf957d2 100644
--- a/libstdc++-v3/include/backward/hashtable.h
+++ b/libstdc++-v3/include/backward/hashtable.h
@@ -63,6 +63,7 @@
 #include <iterator>
 #include <algorithm>
 #include <bits/stl_function.h>
+#include <ext/alloc_traits.h>
 #include <backward/hash_fun.h>
 
 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
@@ -280,14 +281,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       typedef _Hashtable_node<_Val> _Node;
 
     public:
-      typedef typename _Alloc::template rebind<value_type>::other allocator_type;
+      typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
+	rebind<value_type>::other allocator_type;
+
       allocator_type
       get_allocator() const
       { return _M_node_allocator; }
 
     private:
-      typedef typename _Alloc::template rebind<_Node>::other _Node_Alloc;
-      typedef typename _Alloc::template rebind<_Node*>::other _Nodeptr_Alloc;
+      typedef __gnu_cxx::__alloc_traits<allocator_type> _Alloc_traits;
+      typedef typename _Alloc_traits::template rebind<_Node>::other
+	_Node_Alloc;
+      typedef typename _Alloc_traits::template rebind<_Node*>::other
+	_Nodeptr_Alloc;
       typedef std::vector<_Node*, _Nodeptr_Alloc> _Vector_type;
 
       _Node_Alloc _M_node_allocator;
@@ -608,7 +614,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	__n->_M_next = 0;
 	__try
 	  {
-	    this->get_allocator().construct(&__n->_M_val, __obj);
+	    allocator_type __a = this->get_allocator();
+	    _Alloc_traits::construct(__a, &__n->_M_val, __obj);
 	    return __n;
 	  }
 	__catch(...)
@@ -621,7 +628,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       void
       _M_delete_node(_Node* __n)
       {
-	this->get_allocator().destroy(&__n->_M_val);
+	allocator_type __a = this->get_allocator();
+	_Alloc_traits::destroy(__a, &__n->_M_val);
 	_M_put_node(__n);
       }
       
diff --git a/libstdc++-v3/include/ext/alloc_traits.h b/libstdc++-v3/include/ext/alloc_traits.h
index 65fa3de271c..65340eb7acc 100644
--- a/libstdc++-v3/include/ext/alloc_traits.h
+++ b/libstdc++-v3/include/ext/alloc_traits.h
@@ -132,6 +132,11 @@ template<typename _Alloc, typename = typename _Alloc::value_type>
     allocate(_Alloc& __a, size_type __n)
     { return __a.allocate(__n); }
 
+    template<typename _Hint>
+      _GLIBCXX_NODISCARD static pointer
+      allocate(_Alloc& __a, size_type __n, _Hint __hint)
+      { return __a.allocate(__n, __hint); }
+
     static void deallocate(_Alloc& __a, pointer __p, size_type __n)
     { __a.deallocate(__p, __n); }
 
diff --git a/libstdc++-v3/include/ext/extptr_allocator.h b/libstdc++-v3/include/ext/extptr_allocator.h
index 2ce87b60c44..4e86fef81e4 100644
--- a/libstdc++-v3/include/ext/extptr_allocator.h
+++ b/libstdc++-v3/include/ext/extptr_allocator.h
@@ -92,8 +92,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       const_pointer address(const_reference __x) const _GLIBCXX_NOEXCEPT
       { return std::__addressof(__x); }
 
-      _GLIBCXX_NODISCARD pointer allocate(size_type __n, void* __hint = 0)
-      { return _M_real_alloc.allocate(__n,__hint); }
+      _GLIBCXX_NODISCARD pointer allocate(size_type __n, const void* = 0)
+      { return _M_real_alloc.allocate(__n); }
 
       void deallocate(pointer __p, size_type __n)
       { _M_real_alloc.deallocate(__p.get(), __n); }
diff --git a/libstdc++-v3/include/ext/slist b/libstdc++-v3/include/ext/slist
index 93522caf1f6..cbdfae0bd90 100644
--- a/libstdc++-v3/include/ext/slist
+++ b/libstdc++-v3/include/ext/slist
@@ -49,6 +49,7 @@
 #include <bits/stl_construct.h>
 #include <bits/stl_uninitialized.h>
 #include <bits/concept_check.h>
+#include <ext/alloc_traits.h>
 
 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
 {
@@ -251,7 +252,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	_Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
 	_Slist_node_base* __next_next = __next->_M_next;
 	__pos->_M_next = __next_next;
-	get_allocator().destroy(&__next->_M_data);
+	allocator_type __a = get_allocator();
+	__alloc_traits<allocator_type>::destroy(__a, &__next->_M_data);
 	_M_put_node(__next);
 	return __next_next;
       }
@@ -268,7 +270,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	{
 	  _Slist_node<_Tp>* __tmp = __cur;
 	  __cur = (_Slist_node<_Tp>*) __cur->_M_next;
-	  get_allocator().destroy(&__tmp->_M_data);
+	  allocator_type __a = get_allocator();
+	  __alloc_traits<allocator_type>::destroy(__a, &__tmp->_M_data);
 	  _M_put_node(__tmp);
 	}
       __before_first->_M_next = __last_node;
@@ -318,7 +321,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	_Node* __node = this->_M_get_node();
 	__try
 	  {
-	    get_allocator().construct(&__node->_M_data, __x);
+	    allocator_type __a = get_allocator();
+	    __alloc_traits<allocator_type>::construct(__a, &__node->_M_data,
+						      __x);
 	    __node->_M_next = 0;
 	  }
 	__catch(...)
@@ -335,7 +340,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	_Node* __node = this->_M_get_node();
 	__try
 	  {
-	    get_allocator().construct(&__node->_M_data, value_type());
+	    allocator_type __a = get_allocator();
+	    __alloc_traits<allocator_type>::construct(__a, &__node->_M_data,
+						      value_type());
 	    __node->_M_next = 0;
 	  }
 	__catch(...)
@@ -481,7 +488,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       {
 	_Node* __node = (_Node*) this->_M_head._M_next;
 	this->_M_head._M_next = __node->_M_next;
-	get_allocator().destroy(&__node->_M_data);
+	allocator_type __a = get_allocator();
+	__alloc_traits<allocator_type>::destroy(__a, &__node->_M_data);
 	this->_M_put_node(__node);
       }
 
diff --git a/libstdc++-v3/include/tr1/hashtable.h b/libstdc++-v3/include/tr1/hashtable.h
index 354d7b814eb..e6455ebc3f2 100644
--- a/libstdc++-v3/include/tr1/hashtable.h
+++ b/libstdc++-v3/include/tr1/hashtable.h
@@ -126,6 +126,8 @@ namespace tr1
 					    __constant_iterators,
 					    __unique_keys> >
     {
+      typedef __gnu_cxx::__alloc_traits<_Allocator> _Alloc_traits;
+
     public:
       typedef _Allocator                                  allocator_type;
       typedef _Value                                      value_type;
@@ -135,10 +137,10 @@ namespace tr1
       // hasher, if present, comes from _Hash_code_base.
       typedef typename _Allocator::difference_type        difference_type;
       typedef typename _Allocator::size_type              size_type;
-      typedef typename _Allocator::pointer                pointer;
-      typedef typename _Allocator::const_pointer          const_pointer;
-      typedef typename _Allocator::reference              reference;
-      typedef typename _Allocator::const_reference        const_reference;
+      typedef typename _Alloc_traits::pointer             pointer;
+      typedef typename _Alloc_traits::const_pointer       const_pointer;
+      typedef typename _Alloc_traits::reference           reference;
+      typedef typename _Alloc_traits::const_reference     const_reference;
 
       typedef __detail::_Node_iterator<value_type, __constant_iterators,
 				       __cache_hash_code>
@@ -162,13 +164,13 @@ namespace tr1
 
     private:
       typedef __detail::_Hash_node<_Value, __cache_hash_code> _Node;
-      typedef typename _Allocator::template rebind<_Node>::other
-							_Node_allocator_type;
-      typedef typename _Allocator::template rebind<_Node*>::other
-							_Bucket_allocator_type;
+      typedef typename _Alloc_traits::template rebind<_Node>::other
+						      _Node_allocator_type;
+      typedef typename _Alloc_traits::template rebind<_Node*>::other
+						      _Bucket_allocator_type;
 
-      typedef typename _Allocator::template rebind<_Value>::other
-							_Value_allocator_type;
+      typedef typename _Alloc_traits::template rebind<_Value>::other
+						      _Value_allocator_type;
 
       _Node_allocator_type   _M_node_allocator;
       _Node**                _M_buckets;
@@ -259,7 +261,10 @@ namespace tr1
 
       size_type
       max_size() const
-      { return _M_node_allocator.max_size(); }
+      {
+	typedef __gnu_cxx::__alloc_traits<_Node_allocator_type> _Traits;
+	return _Traits::max_size(_M_node_allocator);
+      }
 
       // Observers
       key_equal
diff --git a/libstdc++-v3/include/tr1/regex b/libstdc++-v3/include/tr1/regex
index 73c610d2bbd..01c85e99623 100644
--- a/libstdc++-v3/include/tr1/regex
+++ b/libstdc++-v3/include/tr1/regex
@@ -1789,7 +1789,7 @@ namespace regex_constants
        */
       //@{
       typedef sub_match<_Bi_iter>                             value_type;
-      typedef typename _Allocator::const_reference            const_reference;
+      typedef typename _Base_type::const_reference            const_reference;
       typedef const_reference                                 reference;
       typedef typename _Base_type::const_iterator             const_iterator;
       typedef const_iterator                                  iterator;
diff --git a/libstdc++-v3/testsuite/backward/hash_map/23528.cc b/libstdc++-v3/testsuite/backward/hash_map/23528.cc
index 01ff4bb42b2..712a65d11b9 100644
--- a/libstdc++-v3/testsuite/backward/hash_map/23528.cc
+++ b/libstdc++-v3/testsuite/backward/hash_map/23528.cc
@@ -30,9 +30,13 @@ void test01()
 
   __gnu_cxx::hash_map<int, int>::value_type *y = a.allocate(1);
 
+#if __cplusplus >= 201103L
+  std::allocator_traits<decltype(a)>::construct(a, y, *m.begin());
+  std::allocator_traits<decltype(a)>::destroy(a, y);
+#else
   a.construct(y, *m.begin());
-
   a.destroy(y);
+#endif
   a.deallocate(y, 1);
 }
 
diff --git a/libstdc++-v3/testsuite/tr1/6_containers/unordered_map/capacity/29134-map.cc b/libstdc++-v3/testsuite/tr1/6_containers/unordered_map/capacity/29134-map.cc
index 78febb07e06..0e01c0411fb 100644
--- a/libstdc++-v3/testsuite/tr1/6_containers/unordered_map/capacity/29134-map.cc
+++ b/libstdc++-v3/testsuite/tr1/6_containers/unordered_map/capacity/29134-map.cc
@@ -19,14 +19,16 @@
 
 #include <tr1/unordered_map>
 #include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
 
 // libstdc++/29134
 void test01()
 {
   std::tr1::unordered_map<int, int> um;
 
-  VERIFY( (um.max_size() == std::allocator<std::tr1::__detail::_Hash_node<
- 	   std::pair<const int, int>, false> >().max_size()));
+  std::allocator<std::tr1::__detail::_Hash_node<std::pair<const int, int>,
+						false> > a;
+  VERIFY( um.max_size() == __gnu_test::max_size(a) );
 }
 
 int main()
diff --git a/libstdc++-v3/testsuite/tr1/6_containers/unordered_multimap/capacity/29134-multimap.cc b/libstdc++-v3/testsuite/tr1/6_containers/unordered_multimap/capacity/29134-multimap.cc
index 6695fb9c5d4..f38c8df5c26 100644
--- a/libstdc++-v3/testsuite/tr1/6_containers/unordered_multimap/capacity/29134-multimap.cc
+++ b/libstdc++-v3/testsuite/tr1/6_containers/unordered_multimap/capacity/29134-multimap.cc
@@ -19,14 +19,16 @@
 
 #include <tr1/unordered_map>
 #include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
 
 // libstdc++/29134
 void test01()
 {
   std::tr1::unordered_multimap<int, int> umm;
 
-  VERIFY( (umm.max_size() == std::allocator<std::tr1::__detail::_Hash_node<
- 	   std::pair<const int, int>, false> >().max_size()) );
+  std::allocator<std::tr1::__detail::_Hash_node<std::pair<const int, int>,
+						false> > a;
+  VERIFY( umm.max_size() == __gnu_test::max_size(a) );
 }
 
 int main()
diff --git a/libstdc++-v3/testsuite/tr1/6_containers/unordered_multiset/capacity/29134-multiset.cc b/libstdc++-v3/testsuite/tr1/6_containers/unordered_multiset/capacity/29134-multiset.cc
index e9da207f1fe..01a2c00c18b 100644
--- a/libstdc++-v3/testsuite/tr1/6_containers/unordered_multiset/capacity/29134-multiset.cc
+++ b/libstdc++-v3/testsuite/tr1/6_containers/unordered_multiset/capacity/29134-multiset.cc
@@ -19,14 +19,15 @@
 
 #include <tr1/unordered_set>
 #include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
 
 // libstdc++/29134
 void test01()
 {
   std::tr1::unordered_multiset<int> ums;
 
-  VERIFY( (ums.max_size() == std::allocator<std::tr1::__detail::_Hash_node<
- 	   int, false> >().max_size()) );
+  std::allocator<std::tr1::__detail::_Hash_node<int, false> > a;
+  VERIFY( ums.max_size() == __gnu_test::max_size(a) );
 }
 
 int main()
diff --git a/libstdc++-v3/testsuite/tr1/6_containers/unordered_set/capacity/29134-set.cc b/libstdc++-v3/testsuite/tr1/6_containers/unordered_set/capacity/29134-set.cc
index 2080aaac01e..5dd133265ea 100644
--- a/libstdc++-v3/testsuite/tr1/6_containers/unordered_set/capacity/29134-set.cc
+++ b/libstdc++-v3/testsuite/tr1/6_containers/unordered_set/capacity/29134-set.cc
@@ -19,14 +19,15 @@
 
 #include <tr1/unordered_set>
 #include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
 
 // libstdc++/29134
 void test01()
 {
   std::tr1::unordered_set<int> us;
 
-  VERIFY( (us.max_size() == std::allocator<std::tr1::__detail::_Hash_node<
- 	   int, false> >().max_size()) );
+  std::allocator<std::tr1::__detail::_Hash_node<int, false> > a;
+  VERIFY( us.max_size() == __gnu_test::max_size(a) );
 }
 
 int main()

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]