From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by sourceware.org (Postfix) with ESMTPS id 8C3E3385702B; Wed, 6 Oct 2021 17:18:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8C3E3385702B Received: by mail-ed1-x52e.google.com with SMTP id g8so12526532edt.7; Wed, 06 Oct 2021 10:18:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:subject:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=dope2N0TxOkQ5HYXaYjyynLXxxscijHDOYtGIqPDdmY=; b=5S4QG87lIbbn8WQkQym6NBvslHckBCyhW3hnoaFzSAeuftAIWk86HH7TAVNQfcJxVH RRb0LQYY9dKPjjCe6fpAVyaHzx/avoAY9yZoWU50b8UuIEBLgo1vjzQNs0kJ/UT+EgwL KqhPfJwhczFF8cdxEll6m95QFas9ncg6GHGuZp3fReNHKqtfiM0zg/lBcQWJpZbh6y9M ToTN7FXPLT0xqqj36jrBgNBSP4eED5+BEOYLgAKkhqaqSj+dl6VJ+ayZTdiG+6P6gf59 I34xryHy+3n8uLxVosE4tNnF2hn5dkwBgp6Y5SEPAnszWgXwUnJSWvJWAz/XqxupoIqG HGNw== X-Gm-Message-State: AOAM532IujR3OmOGKAWHnINtB0pff7hcII78TIaBQPDvsRFvR9/qCFA0 7Ua7t1sYZBP/Vghmm9w11UzRxM5W/4w= X-Google-Smtp-Source: ABdhPJwKza+YS6jXm3J1mY4dtIVqI4X+HfHYn3q6nuEXdP5xc0DcB4eQbp+MzuOl4Kdpk7bLMEu/Qg== X-Received: by 2002:a50:fc06:: with SMTP id i6mr19691407edr.370.1633540728922; Wed, 06 Oct 2021 10:18:48 -0700 (PDT) Received: from [10.34.5.219] ([109.190.253.15]) by smtp.googlemail.com with ESMTPSA id l8sm9107294ejn.103.2021.10.06.10.18.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 06 Oct 2021 10:18:48 -0700 (PDT) From: =?UTF-8?Q?Fran=c3=a7ois_Dumont?= Subject: Re: [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator> To: Jonathan Wakely Cc: Jonathan Wakely , libstdc++ , gcc-patches References: <57b54891-59ba-394d-3d53-218f5061b8ef@gmail.com> <9ea0770a-54fb-2c17-b527-2797b5fd3b50@gmail.com> <34b34b00-8c96-68c1-e436-ab0f64291c2f@gmail.com> Message-ID: <193032c8-8b9a-3e55-a244-075b0031d758@gmail.com> Date: Wed, 6 Oct 2021 19:18:36 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------3B8D2F481760470470A10C8A" Content-Language: en-US X-Spam-Status: No, score=-9.1 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 06 Oct 2021 17:18:52 -0000 This is a multi-part message in MIME format. --------------3B8D2F481760470470A10C8A Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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++ > 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::iterator for long it produces >>> __normal_iterator>, quite inconsistent even if >>> useless. >>> >>> --------------3B8D2F481760470470A10C8A Content-Type: text/x-patch; charset=UTF-8; name="to_address.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="to_address.patch" 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 as T* rather than T. + // Need to overload __to_address because the pointer_traits primary template + // will deduce element_type of __normal_iterator as T* rather than T. template - 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 - 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 + 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 #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 +#include #include 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(1, 1).begin()); +const int* s = std::__to_address(std::vector(1, 1).cbegin()); --------------3B8D2F481760470470A10C8A--