Skip to content

Commit 9e302c6

Browse files
committed
Add partial implementation of std::to_address() as llvm::to_address()
Summary: Following on from the review for D58088, this patch provides the prerequisite to_address() implementation that's needed to have pointer_iterator support unique_ptr. 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. Reviewers: dblaikie, paquette Reviewed By: dblaikie Subscribers: dexonsmith, kristina, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D58421 llvm-svn: 354491
1 parent 3316eb5 commit 9e302c6

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

llvm/include/llvm/ADT/STLExtras.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,19 @@ bool hasNItemsOrMore(
15761576
return true;
15771577
}
15781578

1579+
/// Returns a raw pointer that represents the same address as the argument.
1580+
///
1581+
/// The late bound return should be removed once we move to C++14 to better
1582+
/// align with the C++20 declaration. Also, this implementation can be removed
1583+
/// once we move to C++20 where it's defined as std::to_addres()
1584+
///
1585+
/// The std::pointer_traits<>::to_address(p) variations of these overloads has
1586+
/// not been implemented.
1587+
template <class Ptr> auto to_address(const Ptr &P) -> decltype(P.operator->()) {
1588+
return P.operator->();
1589+
}
1590+
template <class T> constexpr T *to_address(T *P) { return P; }
1591+
15791592
} // end namespace llvm
15801593

15811594
#endif // LLVM_ADT_STLEXTRAS_H

llvm/unittests/ADT/STLExtrasTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,4 +446,27 @@ TEST(STLExtrasTest, splat) {
446446
EXPECT_FALSE(is_splat(V));
447447
}
448448

449+
TEST(STLExtrasTest, to_address) {
450+
int *V1 = new int;
451+
EXPECT_EQ(V1, to_address(V1));
452+
453+
// Check fancy pointer overload for unique_ptr
454+
std::unique_ptr<int> V2 = make_unique<int>(0);
455+
EXPECT_EQ(V2.get(), to_address(V2));
456+
457+
V2.reset(V1);
458+
EXPECT_EQ(V1, to_address(V2));
459+
V2.release();
460+
461+
// Check fancy pointer overload for shared_ptr
462+
std::shared_ptr<int> V3 = std::make_shared<int>(0);
463+
std::shared_ptr<int> V4 = V3;
464+
EXPECT_EQ(V3.get(), V4.get());
465+
EXPECT_EQ(V3.get(), to_address(V3));
466+
EXPECT_EQ(V4.get(), to_address(V4));
467+
468+
V3.reset(V1);
469+
EXPECT_EQ(V1, to_address(V3));
470+
}
471+
449472
} // namespace

0 commit comments

Comments
 (0)