[llvm] 8073a6b - [ADT] Allow structured bindings on PointerIntPair

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 4 07:21:19 PST 2022


Author: Benjamin Kramer
Date: 2022-12-04T16:16:04+01:00
New Revision: 8073a6bb0c6d899fb66ee2d33357ace79b45cdcf

URL: https://github.com/llvm/llvm-project/commit/8073a6bb0c6d899fb66ee2d33357ace79b45cdcf
DIFF: https://github.com/llvm/llvm-project/commit/8073a6bb0c6d899fb66ee2d33357ace79b45cdcf.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/PointerIntPair.h
    llvm/unittests/ADT/PointerIntPairTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/PointerIntPair.h b/llvm/include/llvm/ADT/PointerIntPair.h
index 119285087957f..9278ccdb47887 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 <std::size_t I, typename PointerTy, unsigned IntBits, typename IntType,
+          typename PtrTraits, typename Info>
+decltype(auto)
+get(const PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info> &Pair) {
+  static_assert(I < 2);
+  if constexpr (I == 0)
+    return Pair.getPointer();
+  else
+    return Pair.getInt();
+}
+
 } // end namespace llvm
 
+namespace std {
+template <typename PointerTy, unsigned IntBits, typename IntType,
+          typename PtrTraits, typename Info>
+struct tuple_size<
+    llvm::PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>>
+    : std::integral_constant<std::size_t, 2> {};
+
+template <std::size_t I, typename PointerTy, unsigned IntBits, typename IntType,
+          typename PtrTraits, typename Info>
+struct tuple_element<
+    I, llvm::PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>>
+    : std::conditional<I == 0, PointerTy, IntType> {};
+} // namespace std
+
 #endif // LLVM_ADT_POINTERINTPAIR_H

diff  --git a/llvm/unittests/ADT/PointerIntPairTest.cpp b/llvm/unittests/ADT/PointerIntPairTest.cpp
index 9e5e0ee1614be..b2790050a242b 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<PointerIntPair<S *, 2, E>>,
                 "trivially copyable");
 }


        


More information about the llvm-commits mailing list