From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1720) id 128A33858415; Wed, 15 Dec 2021 21:29:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 128A33858415 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: =?utf-8?b?RnJhbuCkpeCkiG9pcyBEdW1vbnQ=?= To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-6004] libstdc++: Overload std::__to_address for __gnu_cxx::__normal_iterator. X-Act-Checkin: gcc X-Git-Author: =?utf-8?q?Fran=C3=A7ois_Dumont?= X-Git-Refname: refs/heads/master X-Git-Oldrev: fd43568cc54e17c8b4a845677872c6282bc6dbb7 X-Git-Newrev: 807ad4bc854caea407aaa09993e4a3220290b0f0 Message-Id: <20211215212908.128A33858415@sourceware.org> Date: Wed, 15 Dec 2021 21:29:08 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Dec 2021 21:29:08 -0000 https://gcc.gnu.org/g:807ad4bc854caea407aaa09993e4a3220290b0f0 commit r12-6004-g807ad4bc854caea407aaa09993e4a3220290b0f0 Author: François Dumont Date: Wed Oct 6 06:55:19 2021 +0200 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. Moreover partial specialization of pointer_traits<__normal_iterator> fails to rebind C, so you get incorrect types like __normal_iterator>. In the case of __gnu_debug::_Safe_iterator the to_pointer method is 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 for C++11 to C++17. * include/debug/safe_iterator.h (std::__to_address(const __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<>, _Sequence>&)): New for C++11 to C++17. * 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 __gnu_debug::_Safe_iterator in _GLIBCXX_DEBUG mode. Diff: --- libstdc++-v3/include/bits/stl_iterator.h | 32 ++++++---------------- libstdc++-v3/include/debug/safe_iterator.h | 17 ++++++++++++ .../24_iterators/normal_iterator/to_address.cc | 17 ++++++++++-- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h index 6bd860b803e..ac9342112f4 100644 --- a/libstdc++-v3/include/bits/stl_iterator.h +++ b/libstdc++-v3/include/bits/stl_iterator.h @@ -1349,32 +1349,16 @@ _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. +#if __cplusplus <= 201703L + // 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(); } + 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()); } #endif - }; /** * @addtogroup iterators diff --git a/libstdc++-v3/include/debug/safe_iterator.h b/libstdc++-v3/include/debug/safe_iterator.h index 5584d06de5a..9c821c82e17 100644 --- a/libstdc++-v3/include/debug/safe_iterator.h +++ b/libstdc++-v3/include/debug/safe_iterator.h @@ -1013,6 +1013,23 @@ namespace __gnu_debug } // namespace __gnu_debug +#if __cplusplus >= 201103L && __cplusplus <= 201703L +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().base())) + { return std::__to_address(__it.base().base()); } + +_GLIBCXX_END_NAMESPACE_VERSION +} +#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/24_iterators/normal_iterator/to_address.cc b/libstdc++-v3/testsuite/24_iterators/normal_iterator/to_address.cc index 510d627435f..6afc6540609 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,19 @@ // { 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()); +#include + +char* p __attribute__((unused)) + = std::__to_address(std::string("1").begin()); +const char* q __attribute__((unused)) + = std::__to_address(std::string("2").cbegin()); +int* r __attribute__((unused)) + = std::__to_address(std::vector(1, 1).begin()); +const int* s __attribute__((unused)) + = std::__to_address(std::vector(1, 1).cbegin()); +int* t __attribute__((unused)) + = std::__to_address(std::vector>(1, 1).begin()); +const int* u __attribute__((unused)) + = std::__to_address(std::vector>(1, 1).cbegin());