From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 4BFD53857C5B; Fri, 26 Nov 2021 17:45:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4BFD53857C5B MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-9317] libstdc++: Make std::pointer_traits SFINAE-friendly [PR96416] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: f2255d28538e81cff7a8f5822f9489625fb88e70 X-Git-Newrev: 8d3391d64799d490117ad48432a9ad2cf38b0091 Message-Id: <20211126174559.4BFD53857C5B@sourceware.org> Date: Fri, 26 Nov 2021 17:45:59 +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: Fri, 26 Nov 2021 17:45:59 -0000 https://gcc.gnu.org/g:8d3391d64799d490117ad48432a9ad2cf38b0091 commit r11-9317-g8d3391d64799d490117ad48432a9ad2cf38b0091 Author: Jonathan Wakely Date: Thu Nov 25 23:29:08 2021 +0000 libstdc++: Make std::pointer_traits SFINAE-friendly [PR96416] This is a simplified version of r12-5532 for the release branches. It still removes the problematic static_assert, but rather than making std::pointer_traits completely empty when the element_type can't be deduced, it just disables element_type and pointer_to. Additionally, the pointer_to member is not completely absent when element_type is cv void, it just has an unusable signature. This is sufficient to avoid errors outside the immediate context when trying to use std::to_address. libstdc++-v3/ChangeLog: PR libstdc++/96416 * include/bits/ptr_traits.h (pointer_traits): Remove static_assert checking for valid element_type. (pointer_traits::element_type, pointer_traits::pointer_to): Do not define when element type cannot be deduced. * testsuite/20_util/pointer_traits/lwg3545.cc: New test. * testsuite/20_util/to_address/1_neg.cc: Adjust dg-error line. * testsuite/20_util/to_address/lwg3545.cc: New test. (cherry picked from commit b8018e5c5ec0e9b6948182f13fba47c67b758d8a) Diff: --- libstdc++-v3/include/bits/ptr_traits.h | 60 +++++++------- .../testsuite/20_util/pointer_traits/lwg3545.cc | 96 ++++++++++++++++++++++ libstdc++-v3/testsuite/20_util/to_address/1_neg.cc | 2 +- .../testsuite/20_util/to_address/lwg3545.cc | 37 +++++++++ 4 files changed, 166 insertions(+), 29 deletions(-) diff --git a/libstdc++-v3/include/bits/ptr_traits.h b/libstdc++-v3/include/bits/ptr_traits.h index 653a3580117..ba9dfbc8ab2 100644 --- a/libstdc++-v3/include/bits/ptr_traits.h +++ b/libstdc++-v3/include/bits/ptr_traits.h @@ -45,19 +45,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION class __undefined; - // Given Template return T, otherwise invalid. - template - struct __get_first_arg - { using type = __undefined; }; - - template class _Template, typename _Tp, - typename... _Types> - struct __get_first_arg<_Template<_Tp, _Types...>> - { using type = _Tp; }; - - template - using __get_first_arg_t = typename __get_first_arg<_Tp>::type; - // Given Template and U return Template, otherwise invalid. template struct __replace_first_arg @@ -75,17 +62,44 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using __make_not_void = typename conditional::value, __undefined, _Tp>::type; + template + struct __ptr_traits_elem_1 + { }; + + template class _SomePointer, typename _Tp, + typename... _Args> + struct __ptr_traits_elem_1<_SomePointer<_Tp, _Args...>> + { + using element_type = _Tp; + using pointer = _SomePointer<_Tp, _Args...>; + + static pointer + pointer_to(__make_not_void& __e) + { return pointer::pointer_to(__e); } + }; + + template + struct __ptr_traits_elem : __ptr_traits_elem_1<_Ptr> + { }; + + template + struct __ptr_traits_elem<_Ptr, __void_t> + { + using element_type = typename _Ptr::element_type; + + static _Ptr + pointer_to(__make_not_void& __e) + { return _Ptr::pointer_to(__e); } + }; + /** * @brief Uniform interface to all pointer-like types * @ingroup pointer_abstractions */ template - struct pointer_traits + struct pointer_traits : __ptr_traits_elem<_Ptr> { private: - template - using __element_type = typename _Tp::element_type; - template using __difference_type = typename _Tp::difference_type; @@ -100,10 +114,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// The pointer type. using pointer = _Ptr; - /// The type pointed to. - using element_type - = __detected_or_t<__get_first_arg_t<_Ptr>, __element_type, _Ptr>; - /// The type used to represent the difference between two pointers. using difference_type = __detected_or_t; @@ -111,13 +121,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// A pointer to a different type. template using rebind = typename __rebind<_Ptr, _Up>::type; - - static _Ptr - pointer_to(__make_not_void& __e) - { return _Ptr::pointer_to(__e); } - - static_assert(!is_same::value, - "pointer type defines element_type or is like SomePointer"); }; /** @@ -165,6 +168,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __to_address(const _Ptr& __ptr) { return std::__to_address(__ptr.operator->()); } #else + template constexpr auto __to_address(const _Ptr& __ptr) noexcept diff --git a/libstdc++-v3/testsuite/20_util/pointer_traits/lwg3545.cc b/libstdc++-v3/testsuite/20_util/pointer_traits/lwg3545.cc new file mode 100644 index 00000000000..f34c7e28eaf --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/pointer_traits/lwg3545.cc @@ -0,0 +1,96 @@ +// { dg-do compile { target c++11 } } + +// LWG 3545. std::pointer_traits should be SFINAE-friendly + +#include + +using std::is_same; + +template using void_t = void; + +template class Probe, typename T, typename = void> + struct has_member + : std::false_type { }; + +template class Probe, typename T> + struct has_member>> + : std::true_type { }; + +template + using element_type = typename T::element_type; +template + using pointer = typename T::pointer; +template + using difference_type = typename T::difference_type; +template + using rebind = typename T::template rebind; +template + using pointer_to = decltype(T::pointer_to(std::declval&>())); + +using invalid = std::pointer_traits; +invalid i; // invalid instantiation is not ill-formed + +static_assert( !has_member::value, "" ); +// These members are defined unconditionally in this version of GCC. +// static_assert( !has_member::value, "" ); +// static_assert( !has_member::value, "" ); +static_assert( !has_member::value, "" ); +static_assert( !has_member::value, "" ); + +struct I +{ + // These members should not be used by pointer_traits

::pointer. + using pointer = int; + using difference_type = int; + template using rebind = int; +}; + +using invalid2 = std::pointer_traits; + +static_assert( !has_member::value, "" ); +// These members are defined unconditionally in this version of GCC. +// static_assert( !has_member::value, "" ); +// static_assert( !has_member::value, "" ); +// static_assert( !has_member::value, "" ); +static_assert( !has_member::value, "" ); + +struct P +{ + using element_type = long; + struct pointer { }; // should not be used by pointer_traits

::pointer +}; +using Ptraits = std::pointer_traits

; +Ptraits p; + +static_assert( is_same, long>::value, "" ); +static_assert( is_same, P>::value, "" ); +static_assert( is_same, std::ptrdiff_t>::value, "" ); +static_assert( !has_member::value, "" ); +static_assert( is_same, P>::value, "" ); + +struct V { using element_type = const void; }; +using Vtraits = std::pointer_traits; +Vtraits v; + +static_assert( is_same, const void>::value, "" ); +static_assert( is_same, V>::value, "" ); +static_assert( is_same, std::ptrdiff_t>::value, "" ); +static_assert( !has_member::value, "" ); +static_assert( !has_member::value, "" ); + +template +struct clever_ptr +{ + static T obj; + + static clever_ptr pointer_to(T&) { return {}; } + constexpr T* operator->() const { return &obj; } +}; + +using Ctraits = std::pointer_traits>; + +static_assert( is_same, char>::value, "" ); +static_assert( is_same, clever_ptr>::value, "" ); +static_assert( is_same, std::ptrdiff_t>::value, "" ); +static_assert( is_same, clever_ptr>::value, "" ); +static_assert( is_same, clever_ptr>::value, "" ); diff --git a/libstdc++-v3/testsuite/20_util/to_address/1_neg.cc b/libstdc++-v3/testsuite/20_util/to_address/1_neg.cc index ff3a1fbcb5d..aedff208763 100644 --- a/libstdc++-v3/testsuite/20_util/to_address/1_neg.cc +++ b/libstdc++-v3/testsuite/20_util/to_address/1_neg.cc @@ -17,7 +17,7 @@ // { dg-options "-std=gnu++2a" } // { dg-do compile { target c++2a } } -// { dg-error "not a function pointer" "" { target *-*-* } 158 } +// { dg-error "not a function pointer" "" { target *-*-* } 0 } #include diff --git a/libstdc++-v3/testsuite/20_util/to_address/lwg3545.cc b/libstdc++-v3/testsuite/20_util/to_address/lwg3545.cc new file mode 100644 index 00000000000..a80ac29b2c9 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/to_address/lwg3545.cc @@ -0,0 +1,37 @@ +// { dg-options "-std=gnu++20" } +// { dg-do compile { target c++20 } } + +#include + +template struct nttp_ptr +{ + T* operator->() const { return nullptr; } +}; + +// This gives an error in C++20, which the LWG 3545 resolution should fix: +auto x = std::to_address( nttp_ptr() ); + +template +struct clever_ptr +{ + static T obj; + constexpr T* operator->() const { return &obj; } +}; + +// pointer_traits specialization is valid, but to_address uses operator-> +static_assert( std::to_address(clever_ptr{}) == &clever_ptr::obj ); + +int the_int; + +template<> +struct std::pointer_traits> +{ + using element_type = int; + using difference_type = std::ptrdiff_t; + using pointer = clever_ptr; + + static constexpr int* to_address(pointer p) { return &the_int; } +}; + +// Uses pointer_traits>::to_address +static_assert( std::to_address(clever_ptr{}) == &the_int );