]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Overload std::__to_address for __gnu_cxx::__normal_iterator.
authorFrançois Dumont <fdumont@gcc.gnu.org>
Wed, 6 Oct 2021 04:55:19 +0000 (06:55 +0200)
committerFrançois Dumont <fdumont@gcc.gnu.org>
Wed, 15 Dec 2021 21:28:05 +0000 (22:28 +0100)
Prefer to overload __to_address to partially specialize std::pointer_traits because
std::pointer_traits would be mostly useless. Moreover partial specialization of
pointer_traits<__normal_iterator<P, C>> fails to rebind C, so you get incorrect types
like __normal_iterator<long*, vector<int>>. In the case of __gnu_debug::_Safe_iterator
the to_pointer method is impossible to implement correctly because we are missing
the parent container to associate the iterator to.

libstdc++-v3/ChangeLog:

* include/bits/stl_iterator.h
(std::pointer_traits<__gnu_cxx::__normal_iterator<>>): Remove.
(std::__to_address(const __gnu_cxx::__normal_iterator<>&)): New for C++11 to C++17.
* include/debug/safe_iterator.h
(std::__to_address(const __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<>,
_Sequence>&)): New for C++11 to C++17.
* testsuite/24_iterators/normal_iterator/to_address.cc: Add check on std::vector::iterator
to validate both __gnu_cxx::__normal_iterator<> __to_address overload in normal mode and
__gnu_debug::_Safe_iterator in _GLIBCXX_DEBUG mode.

libstdc++-v3/include/bits/stl_iterator.h
libstdc++-v3/include/debug/safe_iterator.h
libstdc++-v3/testsuite/24_iterators/normal_iterator/to_address.cc

index 6bd860b803eb9a73a4d09afba178ad656381cc76..ac9342112f4070e623be88c2ccc9703332a48c10 100644 (file)
@@ -1349,32 +1349,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
 #if __cplusplus >= 201103L
 
-  // Need to specialize pointer_traits because the primary template will
-  // deduce element_type of __normal_iterator<T*, C> as T* rather than T.
+#if __cplusplus <= 201703L
+  // Need to overload __to_address because the pointer_traits primary template
+  // will deduce element_type of __normal_iterator<T*, C> as T* rather than T.
   template<typename _Iterator, typename _Container>
-    struct pointer_traits<__gnu_cxx::__normal_iterator<_Iterator, _Container>>
-    {
-    private:
-      using _Base = pointer_traits<_Iterator>;
-
-    public:
-      using element_type = typename _Base::element_type;
-      using pointer = __gnu_cxx::__normal_iterator<_Iterator, _Container>;
-      using difference_type = typename _Base::difference_type;
-
-      template<typename _Tp>
-       using rebind = __gnu_cxx::__normal_iterator<_Tp, _Container>;
-
-      static pointer
-      pointer_to(element_type& __e) noexcept
-      { return pointer(_Base::pointer_to(__e)); }
-
-#if __cplusplus >= 202002L
-      static element_type*
-      to_address(pointer __p) noexcept
-      { return __p.base(); }
+    constexpr auto
+    __to_address(const __gnu_cxx::__normal_iterator<_Iterator,
+                                                   _Container>& __it) noexcept
+    -> decltype(std::__to_address(__it.base()))
+    { return std::__to_address(__it.base()); }
 #endif
-    };
 
   /**
    * @addtogroup iterators
index 5584d06de5a650943a975f3c1b9be5e11512e057..9c821c82e174be878e84858db2090c928511b9e3 100644 (file)
@@ -1013,6 +1013,23 @@ namespace __gnu_debug
 
 } // namespace __gnu_debug
 
+#if __cplusplus >= 201103L && __cplusplus <= 201703L
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+  template<typename _Iterator, typename _Container, typename _Sequence>
+    constexpr auto
+    __to_address(const __gnu_debug::_Safe_iterator<
+                __gnu_cxx::__normal_iterator<_Iterator, _Container>,
+                _Sequence>& __it) noexcept
+    -> decltype(std::__to_address(__it.base().base()))
+    { return std::__to_address(__it.base().base()); }
+
+_GLIBCXX_END_NAMESPACE_VERSION
+}
+#endif
+
 #undef _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS
 #undef _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS
 #undef _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS
index 510d627435fae0e4a847ab7dd3bea3b8faeb6891..6afc6540609ce4c2ffac98a1dd7418efd39fd6d5 100644 (file)
@@ -1,6 +1,19 @@
 // { dg-do compile { target { c++11 } } }
 #include <string>
+#include <vector>
 #include <memory>
 
-char* p = std::__to_address(std::string("1").begin());
-const char* q = std::__to_address(std::string("2").cbegin());
+#include <testsuite_allocator.h>
+
+char* p __attribute__((unused))
+  = std::__to_address(std::string("1").begin());
+const char* q __attribute__((unused))
+  = std::__to_address(std::string("2").cbegin());
+int* r __attribute__((unused))
+  = std::__to_address(std::vector<int>(1, 1).begin());
+const int* s __attribute__((unused))
+  = std::__to_address(std::vector<int>(1, 1).cbegin());
+int* t __attribute__((unused))
+  = std::__to_address(std::vector<int, __gnu_test::CustomPointerAlloc<int>>(1, 1).begin());
+const int* u __attribute__((unused))
+  = std::__to_address(std::vector<int, __gnu_test::CustomPointerAlloc<int>>(1, 1).cbegin());