From 8073a6bb0c6d899fb66ee2d33357ace79b45cdcf Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Sun, 4 Dec 2022 16:10:00 +0100 Subject: [PATCH] [ADT] Allow structured bindings on PointerIntPair Apart from simplifying code, this has the advantage of making the interface between std::pair and PointerIntPair more uniform. --- llvm/include/llvm/ADT/PointerIntPair.h | 26 ++++++++++++++++++++++++++ llvm/unittests/ADT/PointerIntPairTest.cpp | 4 ++++ 2 files changed, 30 insertions(+) diff --git a/llvm/include/llvm/ADT/PointerIntPair.h b/llvm/include/llvm/ADT/PointerIntPair.h index 1192850..9278ccd 100644 --- a/llvm/include/llvm/ADT/PointerIntPair.h +++ b/llvm/include/llvm/ADT/PointerIntPair.h @@ -227,6 +227,32 @@ struct PointerLikeTypeTraits< PtrTraits::NumLowBitsAvailable - IntBits; }; +// Allow structured bindings on PointerIntPair. +template +decltype(auto) +get(const PointerIntPair &Pair) { + static_assert(I < 2); + if constexpr (I == 0) + return Pair.getPointer(); + else + return Pair.getInt(); +} + } // end namespace llvm +namespace std { +template +struct tuple_size< + llvm::PointerIntPair> + : std::integral_constant {}; + +template +struct tuple_element< + I, llvm::PointerIntPair> + : std::conditional {}; +} // namespace std + #endif // LLVM_ADT_POINTERINTPAIR_H diff --git a/llvm/unittests/ADT/PointerIntPairTest.cpp b/llvm/unittests/ADT/PointerIntPairTest.cpp index 9e5e0ee..b279005 100644 --- a/llvm/unittests/ADT/PointerIntPairTest.cpp +++ b/llvm/unittests/ADT/PointerIntPairTest.cpp @@ -62,6 +62,10 @@ TEST(PointerIntPairTest, GetSet) { EXPECT_EQ(&s, Pair2.getPointer()); EXPECT_EQ(E::Case3, Pair2.getInt()); + auto [Pointer2, Int2] = Pair2; + EXPECT_EQ(Pair2.getPointer(), Pointer2); + EXPECT_EQ(Pair2.getInt(), Int2); + static_assert(std::is_trivially_copyable_v>, "trivially copyable"); } -- 2.7.4