Index: llvm/trunk/include/llvm/ADT/STLExtras.h =================================================================== --- llvm/trunk/include/llvm/ADT/STLExtras.h +++ llvm/trunk/include/llvm/ADT/STLExtras.h @@ -1576,6 +1576,19 @@ return true; } +/// Returns a raw pointer that represents the same address as the argument. +/// +/// The late bound return should be removed once we move to C++14 to better +/// align with the C++20 declaration. 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 auto to_address(const Ptr &P) -> decltype(P.operator->()) { + return P.operator->(); +} +template constexpr T *to_address(T *P) { return P; } + } // end namespace llvm #endif // LLVM_ADT_STLEXTRAS_H Index: llvm/trunk/unittests/ADT/STLExtrasTest.cpp =================================================================== --- llvm/trunk/unittests/ADT/STLExtrasTest.cpp +++ llvm/trunk/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 V2 = make_unique(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 V3 = std::make_shared(0); + std::shared_ptr 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