public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "François Dumont" <frs.dumont@gmail.com>
To: Jonathan Wakely <jwakely.gcc@gmail.com>
Cc: Jonathan Wakely <jwakely@redhat.com>,
	libstdc++ <libstdc++@gcc.gnu.org>,
	gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I,C>>
Date: Wed, 6 Oct 2021 19:18:36 +0200	[thread overview]
Message-ID: <193032c8-8b9a-3e55-a244-075b0031d758@gmail.com> (raw)
In-Reply-To: <CAH6eHdTFjVv=q10Qe4Y+LOnOmcOTqeCBFROjaOpXWKN4dH8xKQ@mail.gmail.com>

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

Here is another proposal with the __to_address overload.

I preferred to let it open to any kind of __normal_iterator 
instantiation cause afaics std::vector supports fancy pointer types. It 
is better if __to_address works fine also in this case, no ?

     libstdc++: Overload std::__to_address for __gnu_cxx::__normal_iterator.

     Prefer to overload __to_address to partially specialize 
std::pointer_traits because
     std::pointer_traits would be mostly useless. In the case of 
__gnu_debug::_Safe_iterator
     the to_pointer method is even 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.
             * include/debug/safe_iterator.h
             (std::__to_address(const 
__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<>, _Sequence>&)):
             New.
             * 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 the

Tested under Linux x86_64.

Ok to commit ?

François


On 04/10/21 10:30 pm, Jonathan Wakely wrote:
> On Mon, 4 Oct 2021 at 21:28, François Dumont via Libstdc++
> <libstdc++@gcc.gnu.org>  wrote:
>> On 04/10/21 10:05 pm, François Dumont wrote:
>>> On 02/10/21 10:24 pm, Jonathan Wakely wrote:
>>>> On Sat, 2 Oct 2021 at 18:27, François Dumont wrote:
>>>>> I would like to propose this alternative approach.
>>>>>
>>>>> In this patch I make __normal_iterator and random iterator
>>>>> _Safe_iterator compatible for pointer_traits primary template.
>>>>>
>>>>> Regarding pointer_traits I wonder if it shouldn't check for the
>>>>> to_pointer method availability and use per default: return {
>>>>> std::addressof(__e) }; otherwise. This way we wouldn't have to
>>>>> provide a
>>>>> pointer_to method on __normal_iterator.
>>>> But I would rather not have these members present in vector::iterator
>>>> and string::iterator, in case users accidentally start to rely on them
>>>> being present.
>>> Making pointer_traits friends would help but I do not like it neither.
>>>
>>>
>>>> Another option would be to overload std::__to_address so it knows how
>>>> to get the address from __normal_iterator and _Safe_iterator.
>>>>
>>>> .
>>> I start thinking that rather than proposing not-useful and even
>>> incorrect code in the case of the _Safe_iterator<> it might be a
>>> better approach.
>>>
>>> Even the rebind for __normal_iterator is a little strange because when
>>> doing rebind on std::vector<int>::iterator for long it produces
>>> __normal_iterator<long*, std::vector<int>>, quite inconsistent even if
>>> useless.
>>>
>>>

[-- Attachment #2: to_address.patch --]
[-- Type: text/x-patch, Size: 3255 bytes --]

diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h
index 8afd6756613..6b915ec011c 100644
--- a/libstdc++-v3/include/bits/stl_iterator.h
+++ b/libstdc++-v3/include/bits/stl_iterator.h
@@ -1312,32 +1312,14 @@ _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.
+  // 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(); }
-#endif
-    };
+    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()); }
 
   /**
    * @addtogroup iterators
diff --git a/libstdc++-v3/include/debug/safe_iterator.h b/libstdc++-v3/include/debug/safe_iterator.h
index 5584d06de5a..09e35f79067 100644
--- a/libstdc++-v3/include/debug/safe_iterator.h
+++ b/libstdc++-v3/include/debug/safe_iterator.h
@@ -1018,6 +1018,23 @@ namespace __gnu_debug
 #undef _GLIBCXX_DEBUG_VERIFY_EQ_OPERANDS
 #undef _GLIBCXX_DEBUG_VERIFY_OPERANDS
 
+#if __cplusplus >= 201103L
+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()))
+    { return std::__to_address(__it.base()); }
+
+_GLIBCXX_END_NAMESPACE_VERSION
+}
+#endif
+
 #include <debug/safe_iterator.tcc>
 
 #endif
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-10-06 17:18 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
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               ` François Dumont [this message]
2021-10-06 17:25                 ` [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator<I,C>> 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=193032c8-8b9a-3e55-a244-075b0031d758@gmail.com \
    --to=frs.dumont@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely.gcc@gmail.com \
    --cc=jwakely@redhat.com \
    --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).