From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id A10EA3857C60; Thu, 30 Sep 2021 20:24:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A10EA3857C60 Received: by mail-wm1-x329.google.com with SMTP id l18-20020a05600c4f1200b002f8cf606262so9449631wmq.1; Thu, 30 Sep 2021 13:24:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=WKTZ9DhwLiWwqYVo8XWHuQIeD/7c6px0l7nmUmwcxk4=; b=qjK4KlPpO4wMFKuL/pqHufMaC8B6R8vLF8aTB43ZJjLlF/+LiCHxMgAdzLf4JCkLVF bJghqWjhrhor2VaRj078NiYfRrCaNiaM6r3ZTImjemLae9+bT5HlzCVCmfkZA7TuZ+MI kHY8/w0rBJqIL2LvrU1DiE+10QoK6WK9Y+RSE6d3PmK7Ulco37rrCnSM0C87OqhBCo6w rEEEP7MZKplEqb6KAYvWVgNFUg/mhe9E+vJ/Oz15U/OUb+fRn2pVTr/4/iLJulV5WMwO EsaqYZ9s2pXwdcq8U87iGbt0UaFWsaDTv8KE/xPrq6C76XnVYdIhNxJRukFeWuBpFeCi c8ZA== X-Gm-Message-State: AOAM530WtnRUxjZqdxT1TT2uHxdGIO8DhljvptQ5movaAFhrsZt4Kzjs QS/c9kDGB1Qe2aCeHs9owayeWsxTTLs= X-Google-Smtp-Source: ABdhPJwiV5XXpMPjqRiK+hlYRBV3tcKb0+/BcjACNljC+MdE6H7Pgsc2fCLg60rGJyItpdab5u/lTw== X-Received: by 2002:a1c:c905:: with SMTP id f5mr1020369wmb.148.1633033483235; Thu, 30 Sep 2021 13:24:43 -0700 (PDT) Received: from ?IPv6:2a01:e0a:1dc:b1c0:b19c:986f:d20e:a201? ([2a01:e0a:1dc:b1c0:b19c:986f:d20e:a201]) by smtp.googlemail.com with ESMTPSA id w18sm4000104wrt.79.2021.09.30.13.24.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 30 Sep 2021 13:24:42 -0700 (PDT) Subject: Re: [committed] libstdc++: Specialize std::pointer_traits<__normal_iterator> To: libstdc++@gcc.gnu.org References: Cc: gcc-patches From: =?UTF-8?Q?Fran=c3=a7ois_Dumont?= Message-ID: Date: Thu, 30 Sep 2021 22:24:41 +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="------------30A396120C43B2274DCF2CF8" Content-Language: fr X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, NICE_REPLY_A, 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: Thu, 30 Sep 2021 20:24:46 -0000 This is a multi-part message in MIME format. --------------30A396120C43B2274DCF2CF8 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit 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 > > 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. > --------------30A396120C43B2274DCF2CF8 Content-Type: text/plain; charset=UTF-8; name="patch.txt" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="patch.txt" 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 + 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 - 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 + struct pointer_traits<__gnu_debug::_Safe_iterator<_Iterator, _Sequence, + _Category>> + { + private: + using _Base = pointer_traits<_Iterator>; + + template + 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 + 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 +#include using std::is_same; @@ -66,3 +67,13 @@ template }; // PR libstdc++/72793 specialization of pointer_traits is still well-formed: std::pointer_traits>::element_type e; + +static_assert(is_same::iterator, long>>::element_type, + long>::value, + "iterator rebind"); + +static_assert(is_same::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 +#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()); --------------30A396120C43B2274DCF2CF8--