public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: [PATCH] Implement std::to_address for C++2a
@ 2017-11-25 18:48 Glen Fernandes
  2017-11-28 14:57 ` Jonathan Wakely
  0 siblings, 1 reply; 7+ messages in thread
From: Glen Fernandes @ 2017-11-25 18:48 UTC (permalink / raw)
  To: libstdc++, gcc-patches, jwakely

[-- Attachment #1: Type: text/plain, Size: 349 bytes --]

(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

2017-11-25  Glen Joseph Fernandes  <glenjofe@gmail.com>

        * include/bits/ptr_traits.h (to_address): Implement to_address.
        * testsuite/20_util/to_address/1.cc: New tests.

Tested x86_64-pc-linux-gnu.

[-- Attachment #2: to_address.txt --]
[-- Type: text/plain, Size: 4944 bytes --]

commit f0452d8420a8ef82fa611d53cb9b35f0afecd875
Author: Glen Joseph Fernandes <glenjofe@gmail.com>
Date:   Sat Nov 25 10:28:23 2017 -0500

    Implement std::to_address for C++2a
    
    2017-11-25  Glen Joseph Fernandes  <glenjofe@gmail.com>
    
            * include/bits/ptr_traits.h (to_address): Implement to_address.
            * testsuite/20_util/to_address/1.cc: New tests.

diff --git a/libstdc++-v3/include/bits/ptr_traits.h b/libstdc++-v3/include/bits/ptr_traits.h
index 74d4c18126c..67cc7e97a80 100644
--- a/libstdc++-v3/include/bits/ptr_traits.h
+++ b/libstdc++-v3/include/bits/ptr_traits.h
@@ -151,10 +151,49 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     __to_address(_Tp* __ptr) noexcept
     { return __ptr; }
 
+#if __cplusplus <= 201703L
   template<typename _Ptr>
     constexpr typename std::pointer_traits<_Ptr>::element_type*
     __to_address(const _Ptr& __ptr)
     { return std::__to_address(__ptr.operator->()); }
+#else
+  template<typename _Ptr>
+    constexpr auto
+    __to_address(const _Ptr& __ptr) noexcept
+    -> decltype(std::pointer_traits<_Ptr>::to_address(__ptr))
+    { return std::pointer_traits<_Ptr>::to_address(__ptr); }
+
+  template<typename _Ptr, typename... _None>
+    constexpr auto
+    __to_address(const _Ptr& __ptr, _None...) noexcept
+    { return std::__to_address(__ptr.operator->()); }
+
+  /**
+   * @brief Obtain address referenced by a pointer to an object
+   * @param __ptr A pointer to an object
+   * @return @c __ptr
+   * @ingroup pointer_abstractions
+  */
+  template<typename _Tp>
+    constexpr _Tp*
+    to_address(_Tp* __ptr) noexcept
+    {
+      static_assert(!std::is_function_v<_Tp>, "not a pointer to function");
+      return __ptr;
+    }
+
+  /**
+   * @brief Obtain address referenced by a pointer to an object
+   * @param __ptr A pointer to an object
+   * @return @c pointer_traits<_Ptr>::to_address(__ptr) if that expression is
+             well-formed, otherwise @c to_address(__ptr.operator->())
+   * @ingroup pointer_abstractions
+  */
+  template<typename _Ptr>
+    constexpr auto
+    to_address(const _Ptr& __ptr) noexcept
+    { return std::__to_address(__ptr); }
+#endif // C++2a
 
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
diff --git a/libstdc++-v3/testsuite/20_util/to_address/1.cc b/libstdc++-v3/testsuite/20_util/to_address/1.cc
new file mode 100644
index 00000000000..507d20e18a8
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/to_address/1.cc
@@ -0,0 +1,146 @@
+// Copyright (C) 2011-2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+class P1
+{
+public:
+  using element_type = int;
+
+  explicit P1(int* p)
+  : p_(p) { }
+
+  int* operator->() const noexcept
+  { return p_; }
+
+private:
+  int* p_;
+};
+
+class P2
+{
+public:
+  using element_type = int;
+
+  explicit P2(int* p)
+  : p_(p) { }
+
+  P1 operator->() const noexcept
+  { return p_; }
+
+private:
+  P1 p_;
+};
+
+class P3
+{
+public:
+  explicit P3(int* p)
+  : p_(p) { }
+
+  int* get() const noexcept
+  { return p_; }
+
+private:
+  int* p_;
+};
+
+namespace std
+{
+  template<>
+    struct pointer_traits<::P3>
+    {
+      static int* to_address(const ::P3& p) noexcept
+      { return p.get(); }
+    };
+}
+
+class P4
+{
+public:
+  explicit P4(int* p)
+  : p_(p) { }
+
+  int* operator->() const noexcept
+  { return nullptr; }
+
+  int* get() const noexcept
+  { return p_; }
+
+private:
+  int* p_;
+};
+
+namespace std
+{
+  template<>
+    struct pointer_traits<::P4>
+    {
+      static int* to_address(const ::P4& p) noexcept
+      { return p.get(); }
+    };
+}
+
+void test01()
+{
+  int i = 0;
+  int* p = &i;
+  VERIFY( std::to_address(p) == &i );
+}
+
+void test02()
+{
+  int i = 0;
+  P1 p(&i);
+  VERIFY( std::to_address(p) == &i );
+}
+
+void test03()
+{
+  int i = 0;
+  P2 p(&i);
+  VERIFY( std::to_address(p) == &i );
+}
+
+void test04()
+{
+  int i = 0;
+  P3 p(&i);
+  VERIFY( std::to_address(p) == &i );
+}
+
+void test05()
+{
+  int i = 0;
+  P4 p(&i);
+  VERIFY( std::to_address(p) == &i );
+}
+
+int main()
+{
+  test01();
+  test02();
+  test03();
+  test04();
+  test05();
+  return 0;
+}

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Implement std::to_address for C++2a
  2017-11-25 18:48 [PATCH] Implement std::to_address for C++2a Glen Fernandes
@ 2017-11-28 14:57 ` Jonathan Wakely
  2017-11-28 17:39   ` Glen Fernandes
  0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Wakely @ 2017-11-28 14:57 UTC (permalink / raw)
  To: Glen Fernandes; +Cc: libstdc++, gcc-patches

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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Implement std::to_address for C++2a
  2017-11-28 14:57 ` Jonathan Wakely
@ 2017-11-28 17:39   ` Glen Fernandes
  2017-11-28 21:11     ` Jonathan Wakely
  0 siblings, 1 reply; 7+ messages in thread
From: Glen Fernandes @ 2017-11-28 17:39 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: libstdc++, gcc-patches

On Tue, Nov 28, 2017 at 9:24 AM, Jonathan Wakely wrote:
> 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.

Thanks!

> 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:

I was thinking about using if-constexpr with std::is_detected_v but
wondered if it wouldn't be appropriate to use the latter until it
transitions from TS to IS. (But now that you've pointed it out, I
guess an implementation detail like __detected_or_t can live on
forever, even if the detection idiom facilities do not get adopted).

> However, more importantly, both this form and yours fails for the
> following test case, in two ways:
>
> struct P {
>  using F = void();
>
>  F* operator->() const noexcept { return nullptr; }
> };
>
> 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.

Could both be avoided? That is: I don't know if we need to relax it,
or make to_address tolerate it, if the intent is to require the user
to make P a valid pointer-like type  such that pointer_traits<P> is
not ill-formed (by 1. providing an element_type member or 2.
specializing pointer_traits<P>, since P is not a template<class T,
class...> template). Current implementations of __to_address or
__to_raw_pointer that are in use by our library facilities already
have this requirement implicitly (those that use typename
pointer_traits<P>::element_type* as the return type, instead of C++14
auto), so users working with non-raw pointers would already be doing 1
or 2.

> 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.

Ah, yes. I'll move the static_assert into that overload (enabled in
C++2a or higher mode, since it uses is_function_v).

Glen

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Implement std::to_address for C++2a
  2017-11-28 17:39   ` Glen Fernandes
@ 2017-11-28 21:11     ` Jonathan Wakely
  2017-11-29 10:00       ` Glen Fernandes
  0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Wakely @ 2017-11-28 21:11 UTC (permalink / raw)
  To: Glen Fernandes; +Cc: libstdc++, gcc-patches

On 28/11/17 12:30 -0500, Glen Fernandes wrote:
>On Tue, Nov 28, 2017 at 9:24 AM, Jonathan Wakely wrote:
>> 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.
>
>Thanks!
>
>> 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:
>
>I was thinking about using if-constexpr with std::is_detected_v but
>wondered if it wouldn't be appropriate to use the latter until it
>transitions from TS to IS. (But now that you've pointed it out, I
>guess an implementation detail like __detected_or_t can live on
>forever, even if the detection idiom facilities do not get adopted).
>
>> However, more importantly, both this form and yours fails for the
>> following test case, in two ways:
>>
>> struct P {
>>  using F = void();
>>
>>  F* operator->() const noexcept { return nullptr; }
>> };
>>
>> 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.
>
>Could both be avoided? That is: I don't know if we need to relax it,
>or make to_address tolerate it, if the intent is to require the user
>to make P a valid pointer-like type  such that pointer_traits<P> is
>not ill-formed (by 1. providing an element_type member or 2.
>specializing pointer_traits<P>, since P is not a template<class T,
>class...> template). Current implementations of __to_address or
>__to_raw_pointer that are in use by our library facilities already
>have this requirement implicitly (those that use typename
>pointer_traits<P>::element_type* as the return type, instead of C++14
>auto), so users working with non-raw pointers would already be doing 1
>or 2.

OK, that seems reasonable. In that case I think adding a note to the
standard might be useful, to clarify that for the first overload, even
if pointer_traits<Ptr>::to_address(p) is not well-formed, the
specialization pointer_traits<Ptr> must be well-formed (because the
check for to_address(p) can trigger errors outside the immediate
context).

So my test should be changed to have element_type (or specialize
pointer_traits), like so:

#include <memory>

struct P {
  using element_type = void();

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

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

That compiles, and the bug is that is should fail the static assertion.

>> 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.
>
>Ah, yes. I'll move the static_assert into that overload (enabled in
>C++2a or higher mode, since it uses is_function_v).

Great, thanks.

(Using is_function<_Tp>::value as in your original patch would allow
the assertion to be done for all modes that define __to_address)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Implement std::to_address for C++2a
  2017-11-28 21:11     ` Jonathan Wakely
@ 2017-11-29 10:00       ` Glen Fernandes
  2017-11-30 15:10         ` Jonathan Wakely
  0 siblings, 1 reply; 7+ messages in thread
From: Glen Fernandes @ 2017-11-29 10:00 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 307 bytes --]

(Also added a new [_neg] test)

Move static_assert for function pointers to __to_address

2017-11-28  Glen Joseph Fernandes  <glenjofe@gmail.com>

        * include/bits/ptr_traits.h (to_address): Moved static_assert.
        * testsuite/20_util/to_address/1_neg.cc: New test.


Tested x86_64-pc-linux-gnu.

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 3081 bytes --]

commit 0081e4be38f203a0a3d40c66cfa8b1f7b88f8e61
Author: Glen Joseph Fernandes <glenjofe@gmail.com>
Date:   Tue Nov 28 23:56:48 2017 -0500

    Move static_assert for function pointers to __to_address
    
    2017-11-28  Glen Joseph Fernandes  <glenjofe@gmail.com>
    
            * include/bits/ptr_traits.h (to_address): Moved static_assert.
            * testsuite/20_util/to_address/1_neg.cc: New test.

diff --git a/libstdc++-v3/include/bits/ptr_traits.h b/libstdc++-v3/include/bits/ptr_traits.h
index 67cc7e97a80..11a0deb9448 100644
--- a/libstdc++-v3/include/bits/ptr_traits.h
+++ b/libstdc++-v3/include/bits/ptr_traits.h
@@ -147,11 +147,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     using __ptr_rebind = typename pointer_traits<_Ptr>::template rebind<_Tp>;
 
   template<typename _Tp>
     constexpr _Tp*
     __to_address(_Tp* __ptr) noexcept
-    { return __ptr; }
+    {
+      static_assert(!std::is_function<_Tp>::value, "not a function pointer");
+      return __ptr;
+    }
 
 #if __cplusplus <= 201703L
   template<typename _Ptr>
     constexpr typename std::pointer_traits<_Ptr>::element_type*
     __to_address(const _Ptr& __ptr)
@@ -175,14 +178,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    * @ingroup pointer_abstractions
   */
   template<typename _Tp>
     constexpr _Tp*
     to_address(_Tp* __ptr) noexcept
-    {
-      static_assert(!std::is_function_v<_Tp>, "not a pointer to function");
-      return __ptr;
-    }
+    { return std::__to_address(__ptr); }
 
   /**
    * @brief Obtain address referenced by a pointer to an object
    * @param __ptr A pointer to an object
    * @return @c pointer_traits<_Ptr>::to_address(__ptr) if that expression is
diff --git a/libstdc++-v3/testsuite/20_util/to_address/1_neg.cc b/libstdc++-v3/testsuite/20_util/to_address/1_neg.cc
new file mode 100644
index 00000000000..c80013aa930
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/to_address/1_neg.cc
@@ -0,0 +1,36 @@
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+// { dg-error "not a function pointer" "" { target *-*-* } 153 }
+
+#include <memory>
+
+struct P
+{
+  using element_type = void();
+
+  element_type* operator->() const noexcept
+  { return nullptr; }
+};
+
+void test01()
+{
+  P p;
+  std::to_address(p); // { dg-error "required from here" }
+}

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Implement std::to_address for C++2a
  2017-11-29 10:00       ` Glen Fernandes
@ 2017-11-30 15:10         ` Jonathan Wakely
  0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Wakely @ 2017-11-30 15:10 UTC (permalink / raw)
  To: Glen Fernandes; +Cc: libstdc++, gcc-patches

On 29/11/17 04:54 -0500, Glen Fernandes wrote:
>(Also added a new [_neg] test)
>
>Move static_assert for function pointers to __to_address

Thanks, applied.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] Implement std::to_address for C++2a
@ 2017-11-18  8:00 Glen Fernandes
  0 siblings, 0 replies; 7+ messages in thread
From: Glen Fernandes @ 2017-11-18  8:00 UTC (permalink / raw)
  To: libstdc++, gcc-patches; +Cc: jwakely

[-- Attachment #1: Type: text/plain, Size: 194 bytes --]

Implement std::to_address for C++2a

        * include/bits/ptr_traits.h (to_address): Implement to_address.
        * testsuite/20_util/to_address/1.cc: New tests.

Tested x86_64-pc-linux-gnu.

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 4872 bytes --]

commit fffc95183120866461b363b19fe04f269d5f9299
Author: Glen Fernandes <glen.fernandes@gmail.com>
Date:   Fri Nov 17 15:43:19 2017 -0500

    Implement std::to_address for C++2a
    
        * include/bits/ptr_traits.h (to_address): Implement to_address.
        * testsuite/20_util/to_address/1.cc: New tests.

diff --git a/libstdc++-v3/include/bits/ptr_traits.h b/libstdc++-v3/include/bits/ptr_traits.h
index 74d4c18126c..3334c0c9069 100644
--- a/libstdc++-v3/include/bits/ptr_traits.h
+++ b/libstdc++-v3/include/bits/ptr_traits.h
@@ -151,10 +151,49 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     __to_address(_Tp* __ptr) noexcept
     { return __ptr; }
 
+#if __cplusplus <= 201703L
   template<typename _Ptr>
     constexpr typename std::pointer_traits<_Ptr>::element_type*
     __to_address(const _Ptr& __ptr)
     { return std::__to_address(__ptr.operator->()); }
+#else
+  template<typename _Ptr>
+    constexpr auto
+    __to_address(const _Ptr& __ptr) noexcept
+    -> decltype(std::pointer_traits<_Ptr>::to_address(__ptr))
+    { return std::pointer_traits<_Ptr>::to_address(__ptr); }
+
+  template<typename _Ptr, typename... _None>
+    constexpr auto
+    __to_address(const _Ptr& __ptr, _None...) noexcept
+    { return std::__to_address(__ptr.operator->()); }
+
+  /**
+   * @brief Obtain address referenced by a pointer to an object
+   * @param __ptr A pointer to an object
+   * @return @c __ptr
+   * @ingroup pointer_abstractions
+  */
+  template<typename _Tp>
+    constexpr _Tp*
+    to_address(_Tp* __ptr) noexcept
+    {
+      static_assert(!std::is_function<_Tp>::value, "not a function pointer");
+      return __ptr;
+    }
+
+  /**
+   * @brief Obtain address referenced by a pointer to an object
+   * @param __ptr A pointer to an object
+   * @return @c pointer_traits<_Ptr>::to_address(__ptr) if that expression is
+             well-formed, otherwise @c to_address(__ptr.operator->())
+   * @ingroup pointer_abstractions
+  */
+  template<typename _Ptr>
+    constexpr auto
+    to_address(const _Ptr& __ptr) noexcept
+    { return std::__to_address(__ptr); }
+#endif // C++2a
 
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
diff --git a/libstdc++-v3/testsuite/20_util/to_address/1.cc b/libstdc++-v3/testsuite/20_util/to_address/1.cc
new file mode 100644
index 00000000000..507d20e18a8
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/to_address/1.cc
@@ -0,0 +1,146 @@
+// Copyright (C) 2011-2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+class P1
+{
+public:
+  using element_type = int;
+
+  explicit P1(int* p)
+  : p_(p) { }
+
+  int* operator->() const noexcept
+  { return p_; }
+
+private:
+  int* p_;
+};
+
+class P2
+{
+public:
+  using element_type = int;
+
+  explicit P2(int* p)
+  : p_(p) { }
+
+  P1 operator->() const noexcept
+  { return p_; }
+
+private:
+  P1 p_;
+};
+
+class P3
+{
+public:
+  explicit P3(int* p)
+  : p_(p) { }
+
+  int* get() const noexcept
+  { return p_; }
+
+private:
+  int* p_;
+};
+
+namespace std
+{
+  template<>
+    struct pointer_traits<::P3>
+    {
+      static int* to_address(const ::P3& p) noexcept
+      { return p.get(); }
+    };
+}
+
+class P4
+{
+public:
+  explicit P4(int* p)
+  : p_(p) { }
+
+  int* operator->() const noexcept
+  { return nullptr; }
+
+  int* get() const noexcept
+  { return p_; }
+
+private:
+  int* p_;
+};
+
+namespace std
+{
+  template<>
+    struct pointer_traits<::P4>
+    {
+      static int* to_address(const ::P4& p) noexcept
+      { return p.get(); }
+    };
+}
+
+void test01()
+{
+  int i = 0;
+  int* p = &i;
+  VERIFY( std::to_address(p) == &i );
+}
+
+void test02()
+{
+  int i = 0;
+  P1 p(&i);
+  VERIFY( std::to_address(p) == &i );
+}
+
+void test03()
+{
+  int i = 0;
+  P2 p(&i);
+  VERIFY( std::to_address(p) == &i );
+}
+
+void test04()
+{
+  int i = 0;
+  P3 p(&i);
+  VERIFY( std::to_address(p) == &i );
+}
+
+void test05()
+{
+  int i = 0;
+  P4 p(&i);
+  VERIFY( std::to_address(p) == &i );
+}
+
+int main()
+{
+  test01();
+  test02();
+  test03();
+  test04();
+  test05();
+  return 0;
+}

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-11-30 15:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-25 18:48 [PATCH] Implement std::to_address for C++2a Glen Fernandes
2017-11-28 14:57 ` Jonathan Wakely
2017-11-28 17:39   ` Glen Fernandes
2017-11-28 21:11     ` Jonathan Wakely
2017-11-29 10:00       ` Glen Fernandes
2017-11-30 15:10         ` Jonathan Wakely
  -- strict thread matches above, loose matches on Subject: below --
2017-11-18  8:00 Glen Fernandes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).