This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Implement std::to_address for C++2a


On 25/11/17 10:31 -0500, Glen Fernandes wrote:
(Just a minor update to the last patch to use is_function_v<T> instead
of is_function<T>::value)

Implement std::to_address for C++2a

Thanks, Glen, I've committed this to trunk, with one small change to
fix the copyright dates in the new test, to be just 2017.

Because my new hobby is finding uses for if-constexpr, I think we
could have used the detection idiom to do it in a single overload, but
I don't see any reason to prefer that over your implementation:

 template<typename _Ptr>
   using __ptr_traits_to_address
     = decltype(pointer_traits<_Ptr>::to_address(std::declval<_Ptr>()));

 template<typename _Ptr>
   constexpr auto
   __to_address(const _Ptr& __ptr) noexcept
   {
     struct __nonesuch;
     using type = __detected_or_t<__nonesuch, __ptr_traits_to_address, _Ptr>;
     if constexpr (is_same_v<type, __nonesuch>)
	return std::__to_address(__ptr.operator->());
     else
	return std::pointer_traits<_Ptr>::to_address(__ptr);
   }

However, more importantly, both this form and yours fails for the
following test case, in two ways:

#include <memory>

struct P {
 using F = void();

 F* operator->() const noexcept { return nullptr; }
};

int main()
{
 P p;
 std::to_address(p);
}

Firstly, instantiating pointer_traits<P> fails a static assertion
(outside the immediate context, so not SFINAE-able):

In file included from /home/jwakely/gcc/8/include/c++/8.0.0/bits/stl_iterator.h:66:0,
                from /home/jwakely/gcc/8/include/c++/8.0.0/bits/stl_algobase.h:67,
                from /home/jwakely/gcc/8/include/c++/8.0.0/memory:62,
                from toaddr.cc:1:
/home/jwakely/gcc/8/include/c++/8.0.0/bits/ptr_traits.h: In instantiation of ‘struct std::pointer_traits<P>’:
/home/jwakely/gcc/8/include/c++/8.0.0/type_traits:2364:62:   recursively required by substitution of ‘template<class _Default, template<class ...> class _Op, class ... _Args> struct std::__detector<_Default, std::__void_t<_Op<_Args ...> >, _Op, _Args ...> [with _Default = std::__to_address(const _Ptr&) [with _Ptr = P]::__nonesuch; _Op = std::__ptr_traits_to_address; _Args = {P}]’
/home/jwakely/gcc/8/include/c++/8.0.0/type_traits:2364:62:   required by substitution of ‘template<class _Default, template<class ...> class _Op, class ... _Args> using __detected_or_t = typename std::__detected_or<_Default, _Op, _Args ...>::type [with _Default = std::__to_address(const _Ptr&) [with _Ptr = P]::__nonesuch; _Op = std::__ptr_traits_to_address; _Args = {P}]’
/home/jwakely/gcc/8/include/c++/8.0.0/bits/ptr_traits.h:169:78:   required from ‘constexpr auto std::__to_address(const _Ptr&) [with _Ptr = P]’
/home/jwakely/gcc/8/include/c++/8.0.0/bits/ptr_traits.h:200:31:   required from ‘constexpr auto std::to_address(const _Ptr&) [with _Ptr = P]’
toaddr.cc:12:20:   required from here
/home/jwakely/gcc/8/include/c++/8.0.0/bits/ptr_traits.h:114:7: error: static assertion failed: pointer type defines element_type or is like SomePointer<T, Args>
      static_assert(!is_same<element_type, __undefined>::value,
      ^~~~~~~~~~~~~

I'm not sure if this is a bug in our std::pointer_traits, or if the
standard requires the specialization of std::pointer_traits<P> to be
ill-formed (see [pointer.traits.types] p1). We have a problem if it
does require it, and either need to relax the requirements on
pointer_traits, or we need to alter the wording for to_address so that
it doesn't try to use pointer_traits when the specialization would be
ill-formed.

Secondly, if I remove that static_assert from <bits/ptr_traits.h> then
the test compiles, which is wrong, because it calls std::to_address on
a function pointer type. That should be ill-formed. The problem is
that the static_assert(!is_function_v<_Ptr>) is in std::to_address and
the implementation actually uses std::__to_address. So I think we want
the !is_function_v<_Ptr> check to be moved to the __to_address(_Ptr*)
overload.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]