On 02/10/21 12:29 am, Jonathan Wakely wrote:
On Thu, 30 Sept 2021 at 21:27, François Dumont via Libstdc++
<libstd...@gcc.gnu.org> wrote:
Here is the _Safe_iterator one.
Doing so I noticed that pointer_traits rebind for __normal_iterator was
wrong and added tests on it.
Oops, thanks!
For _Safe_iterator maybe I should specialize only when instantiated with
__normal_iterator ? Or maybe limit to random_access_iterator_tag ?
Yes, I think that's a good idea, maybe only for __normal_iterator<T*,
C>. I think that should cover all the cases we actually need to
support.
And could you change the existing pointer_traits<__normal_iterator<I,
C>> partial specialization to only be valid for
__normal_iterator<T*,C> as well? i.e. only for __normal_iterator that
wraps a pointer (which is true for string::iterator, vector::iterator
etc.)
Is the attached patch what you had in mind ?
At least it is passing tests.
Whatever the pointer_to implementation is problematic, we can only
produce singular iterator as I did ifor now.
Hmm, yes. I think that's OK, because I don't think anybody is actually
going to use that function. What matters is that it works with
std::__to_address for C++17 mode, and that doesn't depend on
pointer_to, so it's OK if it doesn't work usefully.
This is why I think that my patch of the unordered containers to support
custom pointers is not so bad because it avoids usage of
pointer_traits::to_pointer :-)
diff --git a/libstdc++-v3/include/bits/stl_iterator.h
b/libstdc++-v3/include/bits/stl_iterator.h
index 8afd6756613..f6dbe562505 100644
--- a/libstdc++-v3/include/bits/stl_iterator.h
+++ b/libstdc++-v3/include/bits/stl_iterator.h
@@ -1314,23 +1314,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Need to specialize pointer_traits because the 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>>
+ template<typename _Tp, typename _Container>
+ struct pointer_traits<__gnu_cxx::__normal_iterator<_Tp*, _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;
+ using element_type = _Tp;
+ using pointer = __gnu_cxx::__normal_iterator<_Tp*, _Container>;
+ using difference_type = ptrdiff_t;
- template<typename _Tp>
- using rebind = __gnu_cxx::__normal_iterator<_Tp, _Container>;
+ template<typename _Up>
+ using rebind = __gnu_cxx::__normal_iterator<_Up*, _Container>;
- static pointer
- pointer_to(element_type& __e) noexcept
- { return pointer(_Base::pointer_to(__e)); }
+ static _GLIBCXX20_CONSTEXPR pointer
+ pointer_to(__make_not_void<element_type>& __r) noexcept
+ { return pointer(std::addressof(__r)); }
#if __cplusplus >= 202002L
static element_type*
diff --git a/libstdc++-v3/include/debug/safe_iterator.h
b/libstdc++-v3/include/debug/safe_iterator.h
index 5584d06de5a..d2469574240 100644
--- a/libstdc++-v3/include/debug/safe_iterator.h
+++ b/libstdc++-v3/include/debug/safe_iterator.h
@@ -1013,6 +1013,45 @@ namespace __gnu_debug
} // namespace __gnu_debug
+#if __cplusplus >= 201103L
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+ template<typename _Tp, typename _Container, typename _Sequence>
+ struct pointer_traits<__gnu_debug::_Safe_iterator<
+ __gnu_cxx::__normal_iterator<_Tp*, _Container>, _Sequence,
+ std::random_access_iterator_tag>>
+ {
+ private:
+ using __base_ptr =
+ __gnu_cxx::__normal_iterator<_Tp*, _Container>;
+
+ public:
+ using element_type = _Tp;
+ using pointer = __gnu_debug::_Safe_iterator<
+ __gnu_cxx::__normal_iterator<_Tp*, _Container>, _Sequence,
+ std::random_access_iterator_tag>;
+ using difference_type = ptrdiff_t;
+
+ template<typename _Up>
+ using rebind = __gnu_debug::_Safe_iterator<
+ __gnu_cxx::__normal_iterator<_Up*, _Container>, _Sequence,
+ std::random_access_iterator_tag>;
+
+ static pointer
+ pointer_to(element_type& __e) noexcept
+ { return { __base_ptr(std::addressof(__e)), nullptr }; }
+
+#if __cplusplus >= 202002L
+ static element_type*
+ to_address(const pointer& __p) noexcept
+ { return __p.base().base(); }
+#endif
+ };
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace
+#endif
+
#undef _GLIBCXX_DEBUG_VERIFY_DIST_OPERANDS
#undef _GLIBCXX_DEBUG_VERIFY_REL_OPERANDS
#undef _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS
diff --git a/libstdc++-v3/testsuite/20_util/pointer_traits/rebind.cc
b/libstdc++-v3/testsuite/20_util/pointer_traits/rebind.cc
index 159ea8f5294..b78e974d777 100644
--- a/libstdc++-v3/testsuite/20_util/pointer_traits/rebind.cc
+++ b/libstdc++-v3/testsuite/20_util/pointer_traits/rebind.cc
@@ -18,6 +18,7 @@
// { dg-do compile { target c++11 } }
#include <memory>
+#include <vector>
using std::is_same;
@@ -66,3 +67,13 @@ template<typename T, int = 0>
};
// PR libstdc++/72793 specialization of pointer_traits is still well-formed:
std::pointer_traits<CannotRebind<int>>::element_type e;
+
+static_assert(is_same<typename std::pointer_traits<
+ Rebind<typename std::vector<int>::iterator, long>>::element_type,
+ long>::value,
+ "iterator rebind");
+
+static_assert(is_same<typename std::pointer_traits<
+ Rebind<typename std::vector<int>::const_iterator,
long>>::element_type,
+ long>::value,
+ "const_iterator rebind");
diff --git a/libstdc++-v3/testsuite/24_iterators/normal_iterator/to_address.cc
b/libstdc++-v3/testsuite/24_iterators/normal_iterator/to_address.cc
index 510d627435f..433c803beb1 100644
--- a/libstdc++-v3/testsuite/24_iterators/normal_iterator/to_address.cc
+++ b/libstdc++-v3/testsuite/24_iterators/normal_iterator/to_address.cc
@@ -1,6 +1,9 @@
// { 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());
+int* r = std::__to_address(std::vector<int>(1, 1).begin());
+const int* s = std::__to_address(std::vector<int>(1, 1).cbegin());