Message ID | 20170720165340.GK15340@redhat.com |
---|---|
State | New |
Headers | show |
On Thu, Jul 20, 2017 at 12:53 PM, Jonathan Wakely wrote: > We have a more general problem with this, which is that if it's only > available for C++2a mode then we can't use the new feature in most of > the library. Which would be very unfortunate. I want to use this! > > In order to clean up the various places in the library that > could/should use to_address I think we need our own version of it. > Something like our std::__addressof which can be used even for C++98, > and is used by the implementation of the C++11 std::addressof. > > The attached patch does something like that. I think I'll commit this, > as it's a small improvement over what we have today. As the design of > P0653 evolves (*) we can revisit the definition of __to_address, to > use pointer_traits, or find user-defined overloads by ADL, or > something else. > > (*) there's an active discussion on the LEWG reflector, and Glen said > he's revising the paper. Sounds good - thanks Jonathan. I'll revise the patch when P0653 evolves (and approaches acceptance), and when I do, I'll follow the suggestions and style that you've mentioned above. Glen
commit da6aa2c4ba47f91127c3caeb7a851d086b16f3bc Author: Jonathan Wakely <jwakely@redhat.com> Date: Thu Jul 20 16:19:53 2017 +0100 Define std::__to_address helper * include/bits/allocated_ptr.h (__allocated_ptr::get): Use __to_address. (__allocated_ptr::_S_raw_ptr): Remove. * include/bits/ptr_traits.h (__to_address): Define new function template. * include/bits/stl_vector.h [__cplusplus >= 201103l] (vector::_M_data_ptr): Use __to_address. diff --git a/libstdc++-v3/include/bits/allocated_ptr.h b/libstdc++-v3/include/bits/allocated_ptr.h index 773b3f5..96d7841 100644 --- a/libstdc++-v3/include/bits/allocated_ptr.h +++ b/libstdc++-v3/include/bits/allocated_ptr.h @@ -82,16 +82,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } /// Get the address that the owned pointer refers to. - value_type* get() { return _S_raw_ptr(_M_ptr); } + value_type* get() { return std::__to_address(_M_ptr); } private: - static value_type* _S_raw_ptr(value_type* __ptr) { return __ptr; } - - template<typename _Ptr> - static auto - _S_raw_ptr(_Ptr __ptr) -> decltype(_S_raw_ptr(__ptr.operator->())) - { return _S_raw_ptr(__ptr.operator->()); } - _Alloc* _M_alloc; pointer _M_ptr; }; diff --git a/libstdc++-v3/include/bits/ptr_traits.h b/libstdc++-v3/include/bits/ptr_traits.h index 797e7fc..51c4a20 100644 --- a/libstdc++-v3/include/bits/ptr_traits.h +++ b/libstdc++-v3/include/bits/ptr_traits.h @@ -146,6 +146,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template<typename _Ptr, typename _Tp> using __ptr_rebind = typename pointer_traits<_Ptr>::template rebind<_Tp>; + template<typename _Tp> + constexpr _Tp* + __to_address(_Tp* __ptr) noexcept + { return __ptr; } + + template<typename _Ptr> + inline _GLIBCXX14_CONSTEXPR + typename std::pointer_traits<_Ptr>::element_type* + __to_address(const _Ptr& __ptr) + { return std::__to_address(__ptr.operator->()); } + _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index 7ee3ce9..49f6974 100644 --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -1561,7 +1561,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER template<typename _Ptr> typename std::pointer_traits<_Ptr>::element_type* _M_data_ptr(_Ptr __ptr) const - { return empty() ? nullptr : std::__addressof(*__ptr); } + { return empty() ? nullptr : std::__to_address(__ptr); } #else template<typename _Up> _Up*