[PATCH] D58421: Add partial implementation of std::to_address() as llvm::to_address()

Daniel Sanders via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 19 17:55:08 PST 2019


dsanders created this revision.
dsanders added reviewers: dblaikie, paquette.
Herald added subscribers: kristina, dexonsmith.
Herald added a project: LLVM.

Following on from the review for D58088 <https://reviews.llvm.org/D58088>, this patch provides the
prerequisite to_address() implementation that's needed to have
pointer_iterator support unique_ptr.

'typename Ptr::element_type *' should be replaced with 'auto' once we move
to C++14 to better align with the C++20 declaration. Until then, this
implementation is limited to fancy pointers with an element_type typedef.
Also, this implementation can be removed once we move to C++20 where it's
defined as std::to_addres()

The std::pointer_traits<>::to_address(p) variations of these overloads has
not been implemented.


Repository:
  rL LLVM

https://reviews.llvm.org/D58421

Files:
  include/llvm/ADT/STLExtras.h
  unittests/ADT/STLExtrasTest.cpp


Index: unittests/ADT/STLExtrasTest.cpp
===================================================================
--- unittests/ADT/STLExtrasTest.cpp
+++ unittests/ADT/STLExtrasTest.cpp
@@ -446,4 +446,27 @@
   EXPECT_FALSE(is_splat(V));
 }
 
+TEST(STLExtrasTest, to_address) {
+  int *V1 = new int;
+  EXPECT_EQ(V1, to_address(V1));
+
+  // Check fancy pointer overload for unique_ptr
+  std::unique_ptr<int> V2 = make_unique<int>(0);
+  EXPECT_EQ(V2.get(), to_address(V2));
+
+  V2.reset(V1);
+  EXPECT_EQ(V1, to_address(V2));
+  V2.release();
+
+  // Check fancy pointer overload for shared_ptr
+  std::shared_ptr<int> V3 = std::make_shared<int>(0);
+  std::shared_ptr<int> V4 = V3;
+  EXPECT_EQ(V3.get(), V4.get());
+  EXPECT_EQ(V3.get(), to_address(V3));
+  EXPECT_EQ(V4.get(), to_address(V4));
+
+  V3.reset(V1);
+  EXPECT_EQ(V1, to_address(V3));
+}
+
 } // namespace
Index: include/llvm/ADT/STLExtras.h
===================================================================
--- include/llvm/ADT/STLExtras.h
+++ include/llvm/ADT/STLExtras.h
@@ -1576,6 +1576,22 @@
   return true;
 }
 
+/// Returns a raw pointer that represents the same address as the argument.
+///
+/// 'typename Ptr::element_type *' should be replaced with 'auto' once we move
+/// to C++14 to better align with the C++20 declaration. Until then, this
+/// implementation is limited to fancy pointers with an element_type typedef.
+/// Also, this implementation can be removed once we move to C++20 where it's
+/// defined as std::to_addres()
+///
+/// The std::pointer_traits<>::to_address(p) variations of these overloads has
+/// not been implemented.
+template <class Ptr>
+typename Ptr::element_type *to_address(const Ptr &P) noexcept {
+  return P.operator->();
+}
+template <class T> constexpr T *to_address(T *P) noexcept { return P; }
+
 } // end namespace llvm
 
 #endif // LLVM_ADT_STLEXTRAS_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58421.187480.patch
Type: text/x-patch
Size: 1870 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190220/e32ffdf8/attachment.bin>


More information about the llvm-commits mailing list