public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: "François Dumont" <frs.dumont@gmail.com>
To: libstdc++@gcc.gnu.org
Cc: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I,C>>
Date: Thu, 30 Sep 2021 22:24:41 +0200	[thread overview]
Message-ID: <d9f14283-121e-208d-32f3-91e7ef91b2a6@gmail.com> (raw)
In-Reply-To: <YVNsDz9xJszYeoNl@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 1094 bytes --]

Here is the _Safe_iterator one.

Doing so I noticed that pointer_traits rebind for __normal_iterator was 
wrong and added tests on it.

For _Safe_iterator maybe I should specialize only when instantiated with 
__normal_iterator ? Or maybe limit to random_access_iterator_tag ?

Whatever the pointer_to implementation is problematic, we can only 
produce singular iterator as I did ifor now.

François

On 28/09/21 9:25 pm, Jonathan Wakely via Libstdc++ wrote:
> This allows std::__to_address to be used with __normal_iterator in
> C++11/14/17 modes. Without the partial specialization the deduced
> pointer_traits::element_type is incorrect, and so the return type of
> __to_address is wrong.
>
> A similar partial specialization is probably needed for
> __gnu_debug::_Safe_iterator.
>
> Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
>
> libstdc++-v3/ChangeLog:
>
> 	* include/bits/stl_iterator.h (pointer_traits): Define partial
> 	specialization for __normal_iterator.
> 	* testsuite/24_iterators/normal_iterator/to_address.cc: New test.
>
> Tested x86_64-linux. Committed to trunk.
>


[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 4139 bytes --]

diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h
index 004d767224d..f7e851718c1 100644
--- a/libstdc++-v3/include/bits/stl_iterator.h
+++ b/libstdc++-v3/include/bits/stl_iterator.h
@@ -1294,13 +1294,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     private:
       using _Base = pointer_traits<_Iterator>;
 
+      template<typename _Tp>
+	using __base_rebind = typename _Base::template rebind<_Tp>;
+
     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>;
+	using rebind =
+	  __gnu_cxx::__normal_iterator<__base_rebind<_Tp>, _Container>;
 
       static pointer
       pointer_to(element_type& __e) noexcept
diff --git a/libstdc++-v3/include/debug/safe_iterator.h b/libstdc++-v3/include/debug/safe_iterator.h
index 5584d06de5a..5461d2b342f 100644
--- a/libstdc++-v3/include/debug/safe_iterator.h
+++ b/libstdc++-v3/include/debug/safe_iterator.h
@@ -1013,6 +1013,44 @@ namespace __gnu_debug
 
 } // namespace __gnu_debug
 
+#if __cplusplus >= 201103L
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+  template<typename _Iterator, typename _Sequence, typename _Category>
+    struct pointer_traits<__gnu_debug::_Safe_iterator<_Iterator, _Sequence,
+						      _Category>>
+    {
+    private:
+      using _Base = pointer_traits<_Iterator>;
+
+      template<typename _Tp>
+	using __base_rebind = typename _Base::template rebind<_Tp>;
+
+    public:
+      using element_type = typename _Base::element_type;
+      using pointer =
+	__gnu_debug::_Safe_iterator<_Iterator, _Sequence, _Category>;
+      using difference_type = typename _Base::difference_type;
+
+      template<typename _Tp>
+	using rebind =
+	  __gnu_debug::_Safe_iterator<__base_rebind<_Tp>, _Sequence, _Category>;
+
+      static pointer
+      pointer_to(element_type& __e) noexcept
+      { return pointer(_Base::pointer_to(__e), nullptr); }
+
+#if __cplusplus >= 202002L
+      static element_type*
+      to_address(pointer __p) noexcept
+      { return __p.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());

  reply	other threads:[~2021-09-30 20:24 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-28 19:25 Jonathan Wakely
2021-09-30 20:24 ` François Dumont [this message]
2021-10-01 22:29   ` [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I, C>> Jonathan Wakely
2021-10-02 13:08     ` [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I,C>> François Dumont
2021-10-02 20:28       ` [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I, C>> Jonathan Wakely
2021-10-02 17:27     ` [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I,C>> François Dumont
2021-10-02 20:24       ` [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I, C>> Jonathan Wakely
2021-10-04 20:05         ` [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I,C>> François Dumont
2021-10-04 20:26           ` François Dumont
2021-10-04 20:30             ` [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I, C>> Jonathan Wakely
2021-10-06 17:18               ` [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I,C>> François Dumont
2021-10-06 17:25                 ` François Dumont
2021-12-14  6:53                   ` François Dumont
2021-12-14 13:12                     ` [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I, C>> Jonathan Wakely
2021-12-15 21:16                       ` [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I,C>> François Dumont
2021-12-15 21:21                         ` [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I, C>> Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d9f14283-121e-208d-32f3-91e7ef91b2a6@gmail.com \
    --to=frs.dumont@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).