glenjofe created this revision.
glenjofe created this object with edit policy "No One".

A tiny patch that implements P0653R0 (or D0653R1 which revises the former with 
only a small editorial change after LEWG review).


Repository:
  rL LLVM

https://reviews.llvm.org/D35470

Files:
  include/memory
  
test/std/utilities/memory/pointer.traits/pointer.traits.functions/to_address.pass.cpp
  test/std/utilities/memory/pointer.traits/to_address.pass.cpp

Index: test/std/utilities/memory/pointer.traits/to_address.pass.cpp
===================================================================
--- test/std/utilities/memory/pointer.traits/to_address.pass.cpp
+++ test/std/utilities/memory/pointer.traits/to_address.pass.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+
+// element_type* pointer_traits<T*>::to_address(pointer p) noexcept;
+
+#include <memory>
+#include <cassert>
+
+int main()
+{
+    int i = 0;
+    assert(std::pointer_traits<int*>::to_address(&i) == &i);
+    assert(std::pointer_traits<const int*>::to_address(&i) == &i);
+    assert(std::pointer_traits<void*>::to_address(&i) == &i);
+}
Index: test/std/utilities/memory/pointer.traits/pointer.traits.functions/to_address.pass.cpp
===================================================================
--- test/std/utilities/memory/pointer.traits/pointer.traits.functions/to_address.pass.cpp
+++ test/std/utilities/memory/pointer.traits/pointer.traits.functions/to_address.pass.cpp
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+
+// element_type* pointer_traits::to_address(pointer p) noexcept;
+
+#include <memory>
+#include <cassert>
+
+struct P1
+{
+  typedef int element_type;
+  int* value;
+  explicit P1(int* ptr) noexcept : value(ptr) { }
+  int* operator->() const noexcept { return value; }
+};
+
+struct P2
+{
+  typedef P1::element_type element_type;
+  P1 value;
+  explicit P2(P1 ptr) noexcept : value(ptr) { }
+  P1 operator->() const noexcept { return value; }
+};
+
+int main()
+{
+    int i = 0;
+    P1 p1(&i);
+    assert(std::pointer_traits<P1>::to_address(p1) == &i);
+    P2 p2(p1);
+    assert(std::pointer_traits<P2>::to_address(p2) == &i);
+}
Index: include/memory
===================================================================
--- include/memory
+++ include/memory
@@ -32,6 +32,7 @@
     template <class U> using rebind = <details>;
 
     static pointer pointer_to(<details>);
+    static element_type* to_address(pointer p) noexcept;
 };
 
 template <class T>
@@ -44,6 +45,7 @@
     template <class U> using rebind = U*;
 
     static pointer pointer_to(<details>) noexcept;
+    static element_type* to_address(pointer p) noexcept;
 };
 
 template <class Alloc>
@@ -950,11 +952,20 @@
 
 private:
     struct __nat {};
+
+    template <class _Tp>
+        _LIBCPP_INLINE_VISIBILITY
+        static element_type* __to_address(_Tp __p) _NOEXCEPT
+            {return pointer_traits<_Tp>::to_address(__p);}
 public:
     _LIBCPP_INLINE_VISIBILITY
     static pointer pointer_to(typename conditional<is_void<element_type>::value,
                                            __nat, element_type>::type& __r)
         {return pointer::pointer_to(__r);}
+
+    _LIBCPP_INLINE_VISIBILITY
+    static element_type* to_address(pointer __p) _NOEXCEPT
+        {return __to_address(__p.operator->());}
 };
 
 template <class _Tp>
@@ -977,6 +988,9 @@
     static pointer pointer_to(typename conditional<is_void<element_type>::value,
                                       __nat, element_type>::type& __r) _NOEXCEPT
         {return _VSTD::addressof(__r);}
+
+    _LIBCPP_INLINE_VISIBILITY
+    static element_type* to_address(pointer __p) _NOEXCEPT {return __p;}
 };
 
 template <class _From, class _To>
@@ -1089,20 +1103,12 @@
 #endif
 };
 
-template <class _Tp>
-inline _LIBCPP_INLINE_VISIBILITY
-_Tp*
-__to_raw_pointer(_Tp* __p) _NOEXCEPT
-{
-    return __p;
-}
-
 template <class _Pointer>
 inline _LIBCPP_INLINE_VISIBILITY
 typename pointer_traits<_Pointer>::element_type*
 __to_raw_pointer(_Pointer __p) _NOEXCEPT
 {
-    return _VSTD::__to_raw_pointer(__p.operator->());
+    return pointer_traits<_Pointer>::to_address(__p);
 }
 
 template <class _Tp, class = void>
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D35470: [libcxx] Im... Glen Fernandes via Phabricator via cfe-commits

Reply via email to